Forum Moderators: phranque

Message Too Old, No Replies

How to Mod Rewrite with 2 conditions

         

Gian04

12:26 pm on Sep 21, 2009 (gmt 0)

10+ Year Member



How can I do this?

If the URL contains a "page" variable it should follow this RewriteRule

RewriteRule ^page([^/\.]+)/?$ index.php?page=$1 [L]

Otherwise it should follow this.

RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?v=$1 [L] 

jdMorgan

12:39 pm on Sep 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Add a RewriteCond to the first rule to test %{QUERY_STRING} for the presence of "page=".
If that condition is met, then the rule will be invoked, and the [L] flag will prevent the second rule from executing.

Note that accepting requested URL-paths with or without a trailing slash will result in duplicate content (two URLs resolving to the same content) -- a problem much-discussed in our Google search forum. This should be avoided by picking no-slash or with-slash URLs as your preferred canonical URL form, and adding a rule to redirect the non-preferred-format URLs to the preferred URLs. Then your two internal rewrite rules should be modified to accept only the canonical form.

Generally, 'pages' should not have a trailing slash, and 'directories' should have a trailing slash.

Jim

Gian04

12:45 pm on Sep 21, 2009 (gmt 0)

10+ Year Member



I have never used RewriteCond before, so how can I write the conditions

Also I dont understand this

picking no-slash or with-slash URLs as your preferred canonical URL form

Did you mean that I should do it this way?
RewriteRule ^page([^/\.]+)?$ index.php?page=$1 [L]

jdMorgan

1:07 pm on Sep 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This may not be clear because of terminology. If you simply want to rewrite all URL-paths starting with "page" using the first rule, rewrite all other alphanumeric+hyphen URL requests with the second rule, and correct the duplicate-content problem I mentioned, then all you need is something like this:

# If requested URL-path ends with a slash but does not resolve to a physically-existing directory
RewriteCond %{REQUEST_FILENAME} !-d
# externally redirect to remove the trailing slash
RewriteRule ^(.+)/$ http://www.example.com/$1 [R=301,L]
#
# If requested URL-path starts with "page" and is not followed by any slashes or periods,
# then rewrite to script with the requested page name specified as "page=" in query string
RewriteRule ^page([^/.]+)$ index.php?page=$1 [L]
#
# Else rewrite URL-paths consisting only of alphanumeric characters or
# hyphens to script with requested URL-path as "v=" in query string
RewriteRule ^([a-z0-9\-]+)$ index.php?v=$1 [NC,L]

Note the use of the [NC] flag to speed up the last rule by ~33%.

If you opt to redirect from trailing-slash to non-trailing slash URLs using the first rule, make sure that all links on your own site specify non-trailing-slash URLs. The rule should be used only to correct linking errors on other sites (including search listings), but all links within your own site should point to the correct URL and should not invoke this redirect.

Jim