Forum Moderators: phranque

Message Too Old, No Replies

Why No Redirection?

         

superdevo

6:11 am on Nov 17, 2009 (gmt 0)

10+ Year Member



Hello everyone,

Would anyone know why the following mod_rewrite code is not redirecting www.example.com/index.php?cat=345&art=1234567 to www.example.com/345/1234567 ?

Options +FollowSymLinks
RewriteEngine On

# Specify acceptable index/root file. You could have a static
# index.html or allow index.php without parameters for root:
DirectoryIndex index.html index.php

# Redirect to remove trailing period or comma from URL request
# with parameters, such as from forum with autolink, and force
# www to always be in the URL:
RewriteCond %{QUERY_STRING} ^(([^&]+&)*)[.,]$
RewriteRule (.*) http://www.example.com/$1?%1 [R=301,L]

# Redirect to remove trailing period or comma from URL request
# with path, such as from forum with autolink, and force www:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^(([^/]+/)*)(\.¦,)$ http://www.example.com/$1 [R=301,L]

# Redirect two-parameter-based index.php¦html? or / URL request
# (with parameters in any order) to folder-based URL format, and
# force www to always be in URL:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.(php¦html?))?(\?[^\ ]*)\ HTTP/ [NC]
RewriteCond %{QUERY_STRING} &?cat=([0-9]{3})&?
RewriteCond %1>%{QUERY_STRING} ^([^>]+)>([^&]*&)*art=([0-9]{7})&?
RewriteRule ^(index\.(php¦html?))?$ http://www.example.com/%1/%3? [R=301,L]

# Force all remaining requests for named index files to drop
# the index file filename, and force www:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*index\.(html?¦php)(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]*/)*)index\.(html?¦php)$ http://www.example.com/$1 [R=301,L]

# General rule to force all non-www URLs to be www URLs.
# This rule must be the last one of the redirects:
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

# Rewrite such that stray parameters or values on any index.html?
# or any index.php URL or on / URL request always fail to the
# 404 page:
RewriteCond %{QUERY_STRING} .
RewriteRule ^(index\.(php¦html?))?$ /this.page.does.not.exist [L]

# Rewrite URL request: www.example.com/345/1234567 to internal
# path: /index.php?cat=345&art=1234567 to serve content:
RewriteRule ^([0-9]{3})/([0-9]{7})$ /index.php?cat=$1&art=$2 [L]

Thanks

[edited by: jdMorgan at 1:01 pm (utc) on Nov. 17, 2009]
[edit reason] Disabled smileys in code. [/edit]

jdMorgan

1:18 pm on Nov 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, it looks like it should work. It should not be necessary to check %{REDIRECT_STATUS} in that rule, though, because the same thing is being accomplished by checking %{THE_REQUEST}; If the URL has been previously-internally-rewritten (by the 'friendly-URL-to-script-filepath rewriterule), then REDIRECT_STATUS will be non-blank *and* the current path (script filepath) will no longer match the client-requested (friendly) URL.

Also note that the first rule is redundant with the second rule; Because query strings are passed through rewriterules by default, it's not necessary to manipulate the query string unless you want to *change* it in some way. You can delete the first rule entirely.

And as demonstrated by the URL-path pattern used in your first rule "[.,]" is a more efficient pattern than "(\.¦,)".

And that brings up the only thing I can think of right now, which is to make sure that all broken pipe "¦" characters in any code copied from this forum are changed to solid pipe characters before use; Posting on this forum modifies the pipe characters.

Jim

superdevo

5:40 pm on Nov 17, 2009 (gmt 0)

10+ Year Member



Great, thanks Jim.

I'm gonna try it out your advice and see what happens.