Forum Moderators: phranque

Message Too Old, No Replies

Need a bit of help with URL rewriting

Newbie to URL rewriting needs some help

         

kektex

11:45 pm on Nov 3, 2006 (gmt 0)

10+ Year Member



Hello all,
this is my first time trying to create some URL rewriting rules.I did a bit of reading about it but it doesnīt seem to work.
Basically, what I want to do is rewrite the following URL in my blog from:
http://example.com/post/trackback
to:
http://example.com/post/
For that I tried this rule:

RewriteRule ^/(.+)/trackback$ /$1/trackback/ [R]
RewriteRule ^/(.+)/trackback/$ /$1/ [R=301,L]

Basically , what I want to do is catch any example.com/post/trackback or /post/trackback/ URL
to the original /post/ URL.
I dont get any errors, the rule just doesnīt do anything and returns the default 200 OK page from wordpress (you can see it for any wordpress blog by accessing the /examplepost/trackback URL).

The other URL I want to rewrite is this one:
http://www.example.com/?q=samplequery
What I want to do with that one is just return a 404.I couldnīt find any redirection rule for a 404.

Finally, here is my .htaccess file so you guys can see what might be wrong.Maybe thereīs another rule that prevents the ones Iīm trying from working:


<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule . /index.php [L]
RewriteRule ^/(.+)/trackback$ /$1/trackback/ [R]
RewriteRule ^/(.+)/trackback/$ /$1/ [R=301,L]

</IfModule>

whoisgregg

1:06 am on Nov 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello kektex,

I would not worry about handling the trailing slash for the trackback. Also, because of the placement of your rules below the all-inclusive rules provided by Wordpress, your rules are never even being checked. The page request for "/whatever/trackback" was already matched and rewritten by the "RewriteRule . /index.php [L]" line.

Here's an idea of how it should be structured to achieve your goal (untested):

<IfModule mod_rewrite.c> 
RewriteEngine On
RewriteBase /
RewriteRule ^/([^/]+)/trackback /$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule . /index.php [L]
</IfModule>

kektex

1:34 am on Nov 4, 2006 (gmt 0)

10+ Year Member



Thanks for your reply whoisgregg!
I also tried your suggestion since I read about the [L] operator somewhere, but that isnīt working either.
I donīt know what might be going on.
The .htaccess is in the root of the domain.Maybe thereīs something Iīm not seeing in the paths Iīm using?

jdMorgan

1:45 am on Nov 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The leading slash in the RewriteRule pattern won't be present in the URL-path 'seen' in .htaccess. Therefore your rule won't match.

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^([^/]+)/trackback /$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

Jim

kektex

2:07 am on Nov 4, 2006 (gmt 0)

10+ Year Member



Thanks jdMorgan the worked great.
The other URL I want to rewrite is this one:
http://www.example.com/?q=samplequery
What I want to do with that one is just return a 404.
Is R=404 valid? Can I just do this:

RewriteRule ^([^/]+)/?q=([^/]+)$ [R=404]

I remember reading on the Apache website about mod_rewrite that R is used for returning the 300-400 codes,but I havent read on any other way how to do this.

jdMorgan

2:20 am on Nov 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



R=404 is not valid.

Instead, simply rewrite to a filepath that does not exist.

To catch query strings, you'll need to use a RewriteCond with the %{QUERY_STRING} variable.

Try a search [webmasterworld.com] on WebmasterWorld to find lots of previous threads.

Jim

[edited by: jdMorgan at 2:21 am (utc) on Nov. 4, 2006]

whoisgregg

7:26 pm on Nov 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The leading slash in the RewriteRule pattern won't be present in the URL-path 'seen' in .htaccess. Therefore your rule won't match.

Doh! Thanks for catching that. :)