I’ve attempted answers from about 10 existing SO questions to no avail. Using Salman A’s answer in Rewrite rule that removes query string from URL and redirect, I created the following .htaccess file:
Options +FollowSymLinks
RewriteBase /
RewriteEngine On
RewriteCond %{QUERY_STRING} !=""
RewriteRule ^(.*) /$1? [R,L]
…with the goal being to take a url like this:
http://www.example.com/index.php?c=web
…and make it look like this:
http://www.example.com/web
After following Salman A’s answer, the urls looked like:
http://www.example.com/?c=web
Then I messed something up while trying to strip the “?c=” part — unfortunately I don’t know exactly what I messed up — and then I deleted the .htaccess file because I didn’t want to mess anything else up. Now, even without the .htaccess file, some of the url’s — notably, the three url’s I clicked while testing Salman A’s answer — still look like:
http://www.example.com/?c=web
…even though I deleted the .htaccess file. Three questions: 1) why are the changes persisting despite deleting the .htaccess file; 2) is that anything I need to worry about; and, 3) how do I modify the .htaccess file to get all the urls to look like:
http://www.example.com/web
UPDATE: this is what I was worried about http://getluky.net/2010/12/14/301-redirects-cannot-be-undon/. If I botch a 301 redirect and new visitors go to the site, are they stuck with the botch? Is there a way to un-cache this?
When you say “take a url like this and make it look like this”, do you mean make it look that way to the server or make it look that way to the browser (two completely different things). See the top part of this answer for some clarification.
The answer that you’ve implemented makes the server see the request without the query string. After the URI
/index.php?c=webmakes its way through the URL/file mapping pipeline, mod_rewrite churns out:/index.php, removing the query string?c=weband finally the web server takes/index.phpand serves it (sans query string). Of course, while all of this is happening, the browser, which knows nothing of all this mod_rewrite magic flying around, still displays the same URL that it was told to display,http://www.example.com/index.php?c=web, and since it’s never told otherwise, that’s the URL you still see.If you want to change the URL that the browser sees, then you need to externally redirect the browser to the new URL. You can use something like this:
So when your browser requests
http://www.example.com/index.php?c=web, this rule will match against the query string, and redirect the browser to the result,/web, thus changing the URL in the URL address bar of the browser. The browser now sees this new URL, makes a brand new request to the server for this new URL. You’re probably going to want to change that back to what it originally was, because I’m guessing you really don’t have a directory or file at/web. So you do this:This makes it so the server sees
/index.php?c=webwhen someone requests/web.