Search code examples
.htaccessmod-rewritehttp-status-code-301

URL Rewrite Conditions & Parameters


----EDIT---

I have just realized that my explanation of the problem was missing an important piece of information.

The URL's should only be redirected if second parameter is present.

So the rule should read:

Redirect any URL that has /d/ in it, ONLY if /d2/ is also found in the URL.

----End Edit__

I have the need to 301 redirect all URL's on a site that contain a specific parameter to the same URL, but with an additional directory included. All of the URL's that require redirection contain a certain directory: /d/ Example:

http://www.mysite.com/category1/d/subcategory1/subdirectory2/

--Should Redirect to --

http://www.mysite.com/newdirectory/category1/d/subcategory1/subdirectory2/

The one thing in common with any of the URLs' requiring redirection is that they all contain a directory /d/ in the URL, which always immediately follows the "category" directory as indicated in bold in sample URL's above. I would then like to insert an additional directory in front of the category directory as indicated in bold in the sample URL's above. The rest of the URL will remain the same.

Can anyone help with this? I'm relatively new to mod_rewrite and realize I can make a big mess if I don't get it right.

Thanks in advance for anyone who can offer hlep

:)


Solution

  • Try this (edited to reflect change in question):

        RewriteEngine On
        RewriteBase /
    
        RewriteCond %{REQUEST_URI} ^.*/d/.* [NC]
        RewriteCond %{REQUEST_URI} ^.*/d2/.* [NC]
        RewriteCond %{REQUEST_URI} !/newdirectory/ [NC]
        RewriteRule ^(.*)/d/(.*)$ http://www.mysite.com/newdirectory/$1/d/$2 [R=301,L]