简体   繁体   中英

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?

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.

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/

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM