Forum Moderators: phranque

Message Too Old, No Replies

Problem with simple RewriteRule

         

jromao

10:32 pm on Jun 22, 2009 (gmt 0)

10+ Year Member



Hey all,

I'm updating a website I did some time ago and I really want to simplify the URL's and make them Search Engine friendly. This is the code I'm using on the .htaccess

Options +FollowSymlinks
RewriteEngine On

RewriteRule ^(.*)\$ http://mysite.com/teste.php?do=$1 [NC]

I want it to 'transform' mysite.com/gallery on the browser to mysite.com/teste.php?do=gallery on the server for example.

But when I try going there it won't show me the gallery but instead it shows me a 404 error (yes, teste.php exists on the root directory).
What's wrong with my code?
P.S. I have applications such as Joomla and wordpress installed using friendly url's on the same server so it probably isn't an Apache error.

Thanks in advance for any answers ;)

g1smd

10:48 pm on Jun 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You have to change the links ON your pages to use the URLs that you want users to 'see' and 'use'. It is those links that define URLs. You then use a rewrite to connect those URL requests to the internal files inside the server.

As coded above, your rule actually produces a 302 redirect TO a parameter-based URL. I suspect you needed an internal rewrite, not a redirect here. Luckily, this question comes up every day here, so there are plenty of prior threads.

As coded, your rule affects all URL requests: not just those for HTML pages, but also robots.txt, all images and CSS and JS files too. That is probably also not what you want.

jdMorgan

11:06 pm on Jun 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As coded, the rule affects only URLs which contain a literal dollar sign... That dollar sign and everything that follows it will be omitted from the redirect target URL...

Jim

jromao

11:35 pm on Jun 22, 2009 (gmt 0)

10+ Year Member



You have to change the links ON your pages to use the URLs that you want users to 'see' and 'use'. It is those links that define URLs. You then use a rewrite to connect those URL requests to the internal files inside the server.

As coded above, your rule actually produces a 302 redirect TO a parameter-based URL. I suspect you needed an internal rewrite, not a redirect here. Luckily, this question comes up every day here, so there are plenty of prior threads.

As coded, your rule affects all URL requests: not just those for HTML pages, but also robots.txt, all images and CSS and JS files too. That is probably also not what you want.


You are right, I completely forget about robots.txt and teste.php itself :S

I also took the $ but it didn't worked.

I feel like this is really complicating all this stuff that should be simple so now I changed it to:


RewriteRule ^gallery/([0-9]+) http://example.com/teste.php?do=gallery&param1=$1 [NC]
RewriteRule ^gallery http://example.com/teste.php?do=gallery [NC]

But now it redirects me to teste.php?do=blahblah instead of processing it on the server

[edited by: jdMorgan at 3:22 am (utc) on June 23, 2009]
[edit reason] example.com [/edit]

g1smd

1:37 am on Jun 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you include a domain name and/or [R] flag in the target, then you do get a redirect.

Add the [L] flag to every rule, and remove the domain names to get a rewrite for each.

jdMorgan

3:21 am on Jun 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem wasn't with the dollar sign "$", it was with the *escaped* dollar sign "\$".

RewriteRule ^gallery/([0-9]+)$ teste.php?do=gallery&param1=$1 [L]
RewriteRule ^gallery$ teste.php?do=gallery [L]

Jim

g1smd

1:41 am on Jun 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I saw the slash but forgot to comment on it.