Forum Topic: Trouble with redirecting all subdirectories

Forum: .htaccess Forum : Redirecting • Posted by Kevin Meronuk • Updated:

I’ve tried setting up a RedirectMatch based on the example if .htaccess Made Easy, but am not having any luck with it.

I’m trying to clean up an issue with a client’s site that’s running an older version of Expression Engine where the original developers left the registration system open with no admin approval required. When I found the issue, they had nearly 14,000 registered users who were listed under http://domain.com/member/####/ where # is the user ID.

I’m trying to set up a 301 redirect so that all of the individual member URLs point back to the home page, but can’t seem to get anything to work.

I’ve tried:

RewriteEngine On
RedirectMatch 301 ^/member/ http://www.domain.com/

and

RewriteEngine On
RedirectMatch 301 ^/member/(.*) http://www.domain.com/

and

RewriteEngine On
RedirectMatch 301 ^/member/(.*)$ http://www.domain.com/

The results I’m getting with this however is that instead of http://www.domain.com/member/3139/ it changes to http://www.domain.com/?member/3139/

Looking at the existing .htaccess rules, I’m assuming that my desired rule is being overwritten by the default EE rules, which are:

# BEGIN Expression Engine Rewrite
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteRule ^(.*)$ /index.php?$1 [L]
RewriteCond %{HTTP_HOST} ^domain.com [NC] 
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301] 
# END Expression Engine Rewrite

I’m not sure how to merge the two so that I can get these 301s to work.

Any suggestions would be very much appreciated.

Thanks

8 Replies to “Trouble with redirecting all subdirectories”

Posted by Jeff Starr

Yes, it looks like the EE rules are interfering. Some things to try:

  1. Move the redirect that you’re trying to implement to either before or after the EE rules and see if either location makes a difference.
  2. Try redirecting based on the query-string, something like this:
<IfModule mod_rewrite.c>
	RewriteEngine On
	RewriteCond %{QUERY_STRING} ^member/(.*) [NC]
	RewriteRule .* http://www.domain.com/? [L]
</IfModule>

Also try placing this technique both before and after the existing EE rules.

Posted by Kevin Meronuk •

Thanks Jeff,

I gave this a try and it is displaying the homepage content now, though it was keeping the same URL http://www.domain.com/member/3139/

I changed [L] to [R=301,L] and then it updated the URL as well as showing the homepage content.

Your help is very much appreciated.

Cheers,

Kevin

Posted by Jules Webb

I’m having a similar issue with an Expression Engine site.

In my situation I am trying to remove the url segment “read”

Example

http://mysite.com/blog/read/my-post-title should redirect to http://mysite.com/blog/my-post-title

I’ve tried

RedirectMatch 301 ^/blog/read/(.*) http://mysite.com/blog/$1

and the result I get is

http://mysite.com/blog/my-post-title?/blog/read/my-post-title

I tried using a variation of the solution you gave Kevin, but it didn’t work at all

<IfModule mod_rewrite.c>
	RewriteEngine On
	RewriteCond %{QUERY_STRING} ^blog/read/(.*) [NC]
	RewriteRule .* http://mysite.com/blog/? [R=301,L]
</IfModule>

Any assistance is very much appreciated!

Jules

Posted by Jeff Starr

Hi Jules,

It looks like the EE rules are interfering.. have you tried the placing the different techniques (whichever you have tried) both before and after (not at the same time though) the existing EE rules? I would definitely experiment with that first and see how it goes..

Posted by Jules Webb

Hi Jeff, thanks for the reply.

I’ve tried

RedirectMatch 301 ^/blog/read/(.*) http://mysite.com/blog/$1

both at the top and bottom of my htaccess file. I get the same results either way.

With the above RedirectMatch the redirect does occur it’s just the trailing ?/blog/read/my-post-title that’s an issue.

Posted by Jeff Starr

I would try using mod_rewrite, as it can remove those pesky post-redirect query strings, like so:

<IfModule mod_rewrite.c>
	RewriteCond %{QUERY_STRING} ^/blog/read/(.*) [NC]
	RewriteRule .* http://mysite.com/blog/%1? [L]
</IfModule>

The trick is to add a question mark on the end of the RewriteRule. Again, I would try this code both before and then after the EE rules.

Posted by Jules Webb

Touch Down!

It works no matter where I placed it in the file. I tested before and after other EE related, trailing slash, and www redirects.

It also works with or with out the http://mysite.com in the RewriteRule. Is there a reason to use the full path?

Thanks Jeff!

Jules

Posted by Jeff Starr

You’re welcome!

And yes, you’re good with or without the domain name in the RewriteRule, which is fine either way in most cases (mostly a matter of preference).

Cheers!