2

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?

2

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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/

1 Comment

perhaps a better regex is ([^/]+)/*?$ because it will account for multiple forward slashes at the end, but I don't know if the server will keep them there :P
1

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.

2 Comments

The regex ^$ doesn't return anything because you're saying: find me the starting character followed by the end character. That would give you nothing, because it is effectively a blank string. Also, you wouldn't want to replace it with dynamic.php because this is a rewrite, and unless all the pages on your site go to dynamic.php, it wouldn't make sense. Good info for php headers though. Also, there are meta tags to redirect, but this wouldn't be rewriting.
although putting that in /dynamic will work for this special case, I think a global .htaccess will be far better than one in every folder. That way you can ensure that further directories will also work just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.