Jump to content

Help with URL rewriting using PHP only?


Atashi

Recommended Posts

No .htaccess, or other means of affected Apache, means your URLs all have to point to an actual PHP file. However you can basically treat one like a directory and it will still work. For example,

/blog/post.php/2013/10/blah-blah-blah
will execute the blog/post.php script (if it exists), and from there you can look in $_SERVER at either

a) the REQUEST_URI to give you the whole path and query string ("/blog/post.php/2013/10/blah-blah-blah"), or

b) the PATH_INFO to get everything after the filename ("/2013/10/blah-blah-blah")

URL Rewriting isn't strictly achieved through PHP alone, it's the web server that accepts and processes the URI request that needs to know what to do with the URL it has been given by the user.

 

If you want to shorten /thevery/long/url.php to /my-custom-url you need to tell the web server how to process that.

 

If you wanted a basic example, here is something similar that WordPress uses for Apache servers

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

It sends all requests to /index.php where you can then process the URL yourself, through PHP, by accessing the URI through $_SERVER['REQUEST_URI']

URL Rewriting isn't strictly achieved through PHP alone, it's the web server that accepts and processes the URI request that needs to know what to do with the URL it has been given by the user.

 

If you want to shorten /thevery/long/url.php to /my-custom-url you need to tell the web server how to process that.

 

If you wanted a basic example, here is something similar that WordPress uses for Apache servers

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

It sends all requests to /index.php where you can then process the URL yourself, through PHP, by accessing the URI through $_SERVER['REQUEST_URI']

Hi thx for the reply, I believe your example is the codes for .htaccess right? 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.