Thomas
Thomas

Reputation: 5099

Setting custom URLs with PHP

So, I need to set custom URLs so that

http://example.com/dynamic/

goes to:

http://example.com/dynamic.php

I know this is a pretty simple operation but after some googling, I can't find the answer. What is the simplest method for handling something like this?

Upvotes: 2

Views: 6205

Answers (3)

Aram Kocharyan
Aram Kocharyan

Reputation: 20431

The regular expression for that would be something like this:

RewriteEngine on
RewriteRule ([^/]+)/?$ $1.php [L]

Running the regex on the left will return "dynamic/" or "dynamic" if the forward slash is not included. Then it will replace what was captured in the first parenthesized block ("dynamic/") with dynamic.php.

You can try your URL rewrites here:

http://martinmelin.se/rewrite-rule-tester/

And your general regex here:

http://regexpal.com/

Upvotes: 4

evandrix
evandrix

Reputation: 6220

There's several methods of achieving this:-

  1. URL Rewrite (append to .htaccess file on server in /dynamic/ directory)
RewriteEngine on
RewriteRule ^$ dynamic.php
  1. Creating a dummy index.htm/.html/.php file in root folder ie. /dynamic/
    if HTML, use the following javascript:-
<script type="text/javascript">
     window.location = "http://example.com/dynamic.php"
</script>

<script type="text/javascript">
     location.href='http://example.com/dynamic.php';
</script>

<script type="text/javascript">
     location.replace('http://example.com/dynamic.php');
</script>
However, if using PHP instead (recommended), include the following:-

header("Location: http://example.com/dynamic.php");

Remember to use ob_start(), ob_end_flush() to fix the output buffer, if you get the error that the headers are already sent and you can't modify the header information.

Hope this helps.

Upvotes: 1

Vamsi Krishna B
Vamsi Krishna B

Reputation: 11500

you have to do it using url rewriting. that depends on the web server that you are using . for example, you can use .htaccess if your webserver is Apache. Apache URL Rewriting.

Upvotes: 6

Related Questions