Yevgen
Yevgen

Reputation: 797

Using or ( | ) statement in .htaccess rewrite rules

Instead of writing 2 rewrite rules for pointing to the same page, I though I would be able to do this with one rule using the Or statement ( | ):

RewriteRule ^([\d]+)-post[\d]*\.html|#post([\d]+) /post.php?p=$1.html    [R=301,NC,L]

Also tried with an extra $2 at the end since only one will show up anyway:

RewriteRule ^([\d]+)-post[\d]*\.html|#post([\d]+) /post.php?p=$1$2.html    [R=301,NC,L]

With the above rule, this would work fine:

site.com/14729-post9.html

However, this does not work(even though tested here and should work: http://regexpal.com/):

site.com/post.php?p=14729#post14729

Is there actually a way to make it work or do I just have to create 2 separate rules?

BTW, is it more server intense one way or the other?

Upvotes: 0

Views: 87

Answers (2)

Jon Lin
Jon Lin

Reputation: 143906

A major issue here.

The #post14729 part of the URL is called a a URL Fragment, and it is never sent to the server, thus, you can't match against it because it's simply not there. So the second part of your regex: #post([\d]+) will never match.

Upvotes: 0

Ahmed Masud
Ahmed Masud

Reputation: 22402

Okay so you have to understand that the in-document named links such as #post14729 are NEVER passed to the server by the browser. When a link contains an in-document named link, for example: say you click on <a href='http://some.host.com/foo.php?id=1&bar=2#post250'>some link</a>. Here the browser strips the #post250 part when sending the GET to some.host.com and only sends the URL /foo.php with variables id=1 and bar=2. When the result of that GET comes back to the browser, it will search for '#post250' named anchor in the content and scroll to make it visible if needed. The '#post250' cannot be sent back to the browser without some javascript trickery.

Your RewriteEngine isn't really seeing your '#post14729' at all.

Upvotes: 1

Related Questions