Forum Moderators: phranque
I'm working on a website and I'm trying to make it more SEO friendly. So I'm using mod_rewrite to do this.
I'm trying to turn this URL: blablabla.com/products.php?id=tester -> into -> blablabla.com/products/tester/
but I am having no luck. I tried several tutorials and said-to-be proven examples that work all with failure. My site just returns a 404 and doesn't accept my RewriteRules. For some reason, it thinks I'm trying to access a sub-directory and ignores my ".htaccess" file.
Here's the code I was trying to use for the example above:
RewriteEngine on
Option +FollowSymLinks
RewriteRule ^products/([^/]+)/?$ products.php?id=$1 [L]
Any idea on what's going on or what's wrong with it? If someone could help me get something working, I'd greatly appreciate it.
Thanks,
Todd
Have you changed the links on your pages to use the new format URLs?
The Rewrite does not 'make' anything. It merely allows requests for friendly URLs to pull content from file location inside your server where the content really resides.
You also need to decide whether a valid URL request has a trailing slash or not. To allow both to trigger a rewrite is a Duplicate Content issue. Pick one, and redirect the other one with a redirect ahead of the rewrite.
RewriteRule ^products/([^/]+[b])$[/b] [b]/p[/b]roducts.php?id=$1 [L] If you're not careful, the result of your rewrite could be something that matches your pattern and will be rewritten again, in an infinite loop. In that case, you will need a RewriteCond checking THE_REQUEST so the rewrite is invoked only for direct client requests. There's many previous threads with example code for that; several in the last week.
[edited by: g1smd at 4:15 pm (utc) on June 3, 2009]
[edited by: jdMorgan at 7:26 pm (utc) on June 3, 2009]
[edit reason] Closed [ code ] tag. [/edit]
What I'm wondering is if there's a condition or some option I can specify to the mod_rewrite engine to fix it.
It's like telling mod_rewrite to handle requests to /foo/bar. "foo" exists but "bar" is a rewrite rule. Apache thinks I'm accessing "foo/bar" and since it knows I'm not accessing the "foo" directory directly, it ignores my ".htaccess" file even though "bar" does not exist. That's my problem. :-(
I was looking through some of the older posts in this forum but they don't help. :-(
Is there some directive or way to "spoof" Apache to ignore forward-slashes (indicating directories but NOT the trailing slash) and "emulate" it through a rewrite rule?
I was playing around with it and it works fine but whenever I try to write a rule for handling a request with a forward-slash (like a sub-directory) the server ignores the rewrite rules and thinks I'm accessing a real directory or real file, despite it being a rewrite rule in .htaccess.
Would there be some line or directive I'd have to put in the .htaccess file to tell Apache to ignore forward-slashes as sub-directories?
Well I think that's what I needed! Thanks for all your help!
Also, is there a guide to all the .htaccess file directives and parameters? I know Apache has theirs but is there a more condensed version by a 3rd party?