1

I've changed my URL structure to remove the category from the URLs of item pages.

I need some way of redirecting all the old pages to the new pages, using mod_rewrite. I have already a quite elaborate and messy .htaccess file.

What I'd need is to redirect: http://example.com/category-name/item-name/ to this http://example.com/download-item-name/

Right now, I've got this in the htaccess file.

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301]
RewriteRule ^download-([^/]*)/screenshots/$ /screenshots.php?screenshots=$1 [L]
RewriteRule ^download-([^/]*)/screenshots/([^/]*)/$ /screenshots.php?screenshots=$1&shot=$2
#and now without trailing slash, so request will hit script which can do redirect.
RewriteRule ^download-([^/]*)/screenshots/([^/]*)$ /screenshots.php?screenshots=$1&shot=$2 [L]
RewriteRule ^feed/([^/]*)/$ /feed/?category=$1 [L]
RewriteRule ^download-([^/]*)-software/$ /category.php?category=$1 [L] 
#this needs to stay above the below rule
RewriteRule ^download-([^/]*)/$ /software.php?shortname=$1 [QSA]

RewriteRule ^download-([^/]*)/([^/]*)/$ /software.php?shortname=$1&lang=$2 [QSA]

#and now without trailing slash, so request will hit script which can do redirect.
RewriteRule ^download-([^/]*)$ /software.php?shortname=$1 [QSA]

RewriteRule ^download-([^/]*)/([^/]*)$ /software.php?shortname=$1&lang=$2

RewriteRule ^downloading/([^/]*)/$ /download.php?downloading=$1
RewriteRule ^downloading/([^/]*)/([^/]*)/$ /download.php?downloading=$1&lang=$2 [L]
RewriteRule ^download/([^/]*)/([^/]*)/$ /send.php?key=$1&s=$2 [QSA]
RewriteRule ^FQ-([^/]*)/$ /content.php?page=$1 [L]
RewriteCond %{REQUEST_URI} ^(/[^/]+)(/.*)?$
RewriteCond %{DOCUMENT_ROOT}/categories%1/ -d
RewriteRule . /categories%{REQUEST_URI} [QSA]
Options -Indexes
FileETag None
ErrorDocument 404 http://example.com/404/
ErrorDocument 403 http://example.com/

#Some redirects
Redirect 301 /audio-video-players-codecs/zoom-player-max/ http://example.com/download-zoom-player-max/
Redirect 301 /audio-video-players-codecs/zoom-player-premium/ http://example.com/download-zoom-player-premium/
Redirect 301 /audio-video-players-codecs/zoom-player-pro/ http://example.com/download-zoom-player-pro/

1 Answer 1

1

Your .htaccess is messy because of simple things:

Rule #1: avoid mixing Redirect and RewriteRules directives. This will only confuse things. There's always a possibility to replace Redirect by RewriteRules directives.

Rule #2: if you don't need RewriteBase / (which is often the case, because the base is often /), don't write it. This will only confuse things. The shorter the better (that's what she tells me (just kiddin')).

Rule #3:: ^(.*)$ is the same as (.*). Here too, this makes things clearer.

Rule #4:: special rule for you: you can reduce by 2 almost all your RewriteRules using a thing like ^downloading/([^/]*)(/([^/]*))?/ which makes the second argument empty but match both one or two arguments (just valid in your Php that you can get an empty second argument). The shorter the better.

Then to answer your question: to redirect: http://example.com/category-name/item-name/ to this http://example.com/download-item-name/

RewriteRule ^category-([a-zA-Z]+)/item-([a-zA-Z]+)/ ^download-item-$2/ [QSA,R]

This should work


My favorite tool to check for regexp:

http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.