Atashi Posted October 24, 2013 Share Posted October 24, 2013 Can anyone please provide me a example and explanation of URL rewriting with PHP only. I already tested using .htaccess and it works fine. What I want to achieve is to rewrite a URL without using a .htaccess. I read some blogs but still can't understand a bit. Quote Link to comment Share on other sites More sharing options...
requinix Posted October 24, 2013 Share Posted October 24, 2013 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-blahwill 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") Quote Link to comment Share on other sites More sharing options...
Atashi Posted October 24, 2013 Author Share Posted October 24, 2013 Can you provide an example so I can understand what you post much better. thx for the responds anyway. by the way, the url is declared thru a formfor example <form action="thevery/long/url.php>i need to change the url to shorter one. thx agan Quote Link to comment Share on other sites More sharing options...
iarp Posted October 24, 2013 Share Posted October 24, 2013 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'] Quote Link to comment Share on other sites More sharing options...
requinix Posted October 24, 2013 Share Posted October 24, 2013 i need to change the url to shorter one.Move the file. Quote Link to comment Share on other sites More sharing options...
Atashi Posted October 25, 2013 Author Share Posted October 25, 2013 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? Quote Link to comment Share on other sites More sharing options...
iarp Posted October 25, 2013 Share Posted October 25, 2013 Correct Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.