MattM
MattM

Reputation: 3217

Redirect entire sub-directory to root directory

So I am trying to clean up my search engine redirect errors. I had some old development sites that got indexed that I want to redirect to the main site.

Basically I want everything in the /dev/ folder to go to https://myfakewebsite.com/

I added this to .htaccess file in the /dev/ directory:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ /$1 [R=301,L]

I tried this but it doesn't quite work, it does take out the /dev/ but it keeps the rest of the link there.

For example: https://myfakewebsite.com/dev/index.php?route=product/category&path=122

redirects to: https://myfakewebsite.com/index.php?route=product/category&path=122

I want it to redirect to: https://myfakewebsite.com/ (removing the /index.php?route=product/category&path=122 part).

Is there a way to do this with .htaccess?

Upvotes: 0

Views: 1818

Answers (3)

Amit Verma
Amit Verma

Reputation: 41219

You can use the following rule in your /dev/.htaccess

RewriteEngine on


RewriteRule  (.*) / [L,R=301]

The rule above redirects all requests from /dev to the root of site ie . /dev/* => / including query strings from /dev?querystring to /?querystring . By default mod-rewrite appends old query string to the destination path so if you do not want it then you can use QSD flag in your rule or you can another rule to handle and remove QUERY_STRING .

You can use the following rules to redirect urls with and without query string to / . The ? at the end of the rule's destination discards the old query string.

RewriteEngine on

# redirect /dev?querystring to /
RewriteCond %{QUERY_STRING} .+
RewriteRule  (.*) /? [L,R=301]
# redirect /dev/uri to /
RewriteRule  (.*) / [L,R=301]

Make sure to clear your browser cache before testing these redirects.

Upvotes: 1

Kevin Peno
Kevin Peno

Reputation: 9196

You left the match results as part of the redirect on this line:

RewriteRule ^(.*)$ /$1 [R=301,L]

Specifically $1 as part of /$1 (the redirecting portion of the rule). Since this file is within the /dev directory it automatically removes /dev/ then applies the match to the redirect. Since you are using the capture ($1) as part of the rule, it'll take everything it matched after /dev/ (in your rule you should read it as ^/dev/(.*)$) and apply it to /$1, where $1 is whatever it "found" during ^(.*)$ in the previous step.

Try removing the pattern match, e.g.:

RewriteRule ^(.*)$ / [R=301,L]

Additionally, since you aren't using the results of the match you can remove the parenthesis (e.g. ^.*$). You can also put this .htaccess file in the root and simply redirect ^/dev(/?.*)$ to / depending on how you want to manage your subdirectories.

Upvotes: 0

Smuuf
Smuuf

Reputation: 6524

You might want to achieve this kind of "trivial" redirect with the RedirectMatch directive from mod_alias, instead of doing this with mod_rewrite.

RedirectMatch 301 "/dev/(.*)" "/$1"

See documentation at https://httpd.apache.org/docs/2.4/mod/mod_alias.html.

Upvotes: 0

Related Questions