BorysSokolov Posted September 3, 2013 Share Posted September 3, 2013 (edited) Hello. I'd like to remove a GET variable from my URL like so: website.com/works.php?proj=project-name -> website.com/works/project-name. I also need to strip the .php extension from the website.com/works location, even when the proj variable is not present. Thank you for any responses. Edited September 3, 2013 by BorysSokolov Quote Link to comment https://forums.phpfreaks.com/topic/281802-simple-mod_rewrite-for-get-variable/ Share on other sites More sharing options...
Irate Posted September 3, 2013 Share Posted September 3, 2013 Put this inside your .htaccess file. RewriteEngine On # only enter this if this line is not present already RewriteRule ^/works\.php\?(.*?)proj=(.*?)$ /works/$2 Quote Link to comment https://forums.phpfreaks.com/topic/281802-simple-mod_rewrite-for-get-variable/#findComment-1447960 Share on other sites More sharing options...
BorysSokolov Posted September 3, 2013 Author Share Posted September 3, 2013 I tried what you suggested, but I keep getting a 404 error. Quote Link to comment https://forums.phpfreaks.com/topic/281802-simple-mod_rewrite-for-get-variable/#findComment-1447994 Share on other sites More sharing options...
Irate Posted September 4, 2013 Share Posted September 4, 2013 What address are you entering in your URL bar? You should only put in the new, rewritten URL, not the old one. Quote Link to comment https://forums.phpfreaks.com/topic/281802-simple-mod_rewrite-for-get-variable/#findComment-1448113 Share on other sites More sharing options...
requinix Posted September 4, 2013 Share Posted September 4, 2013 RewriteRule does not look at the query string. # 1. If they actually requested "/works.php?proj=foo" then redirect RewriteCond %{REQUEST_URI} ^/works\.php\?proj=([^&]+)$ RewriteRule /works/%1 [L,R] # 2. Rewrite RewriteRule ^/?works/([^/]+) works.php?proj=$1 [L]Even better would be modifying your works.php so that it redirects if it's requested via the wrong URL. Because strictly speaking the #1 above isn't enough to catch the various "invalid" possibilities and simply doing the work in your script is easier than listing them all out. Quote Link to comment https://forums.phpfreaks.com/topic/281802-simple-mod_rewrite-for-get-variable/#findComment-1448148 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.