PDA

View Full Version : Apache 301 Redirect to Hide PHP garbage


drewprops
2012-03-15, 14:55
I recently made the mental leap to understanding how to serve up URLs to mod_rewrite so that I have pretty URLs in some of my simple websites.

I need some help though, when it comes to using a 301 Redirect to 'clean up' the URL when people hit the website from an old PHP-encrusted link.


For example:

Suppose that someone clicks an old-fashioned URL to our site, and that the page still exists at:


example.com/index.php?x=foo


but we want it to look like this:


example.com/foo


in their browser's navigation bar, as it does when clicked upon from any navigation links internal to this website.


I thought that using a redirect in this fashion:


Redirect 301 /index.php?x=foo http://example.com/foo



would work just fine, but of course it does not.


Any insight you folks could provide would be appreciated.

...

turtle
2012-03-15, 16:13
Shouldn't it be a rewrite rule? I'm not great to .htaccess but this seems like what it should be.

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

This is for WP that makes the index.php go away. It's going to be more like this.

turtle
2012-03-15, 16:19
This site says (http://www.iwebtool.com/htaccess_url_rewrite?url=example.com%2Findex.php%3 Fx%3Dfoo&ext=&sep=%2F) to do this:
Options +FollowSymLinks
RewriteEngine on

RewriteRule /x/(.*) index.php?x=$1

Though you might want to remove the "/x" to fit your request.

drewprops
2012-03-15, 16:33
Oh now I've got the rewrite working, that part is EASY....


Options +FollowSymLinks
RewriteEngine on
RewriteRule ^/?([a-zA-Z_0-9\s]+)$ index.php?x=$1 [L]



It's the redirect of a specific "old-fashioned" link that I'm wanting to fix.
I don't even want the visitor to end up SEEING the ugly PHP code, even if they clicked such a link.



...