3

I have a website: www.explainmortgage.com

On that website I have http://www.explainmortgage.com and http://www.explainmortgage.com/mortgage (among many other pages).

http://www.explainmortgage.com and http://www.explainmortgage.com/mortgage are identical so I want to 301 http://www.explainmortgage.com/mortgage to http://www.explainmortgage.com

I use this same codebase for multiple domains and most have a similar situation.

So I want to write one RewriteCond RewriteRule pair that will cover this situation on all my domains using this codebase.

So here is the rule (in psuedo code) that I want:

if ({HTTP_HOST}) contains ({REQUEST_URI} minus the leading /)
then 301 to http://{HTTP_HOST}

examples:

{HTTP_HOST} == 'www.explainmortgage.com'
{REQUEST_URI} == '/mortgage'

{HTTP_HOST} == 'www.explaincars.com'
{REQUEST_URI} == '/cars'

{HTTP_HOST} == 'www.explaineducation.com'
{REQUEST_URI} == '/education'

{HTTP_HOST} == 'www.explainelectronics.com'
{REQUEST_URI} == '/electronics'

So, if there are any regular expression gurus out there that would like to give me a hand, I would greatly appreciate it.

HeatfanJohn
  • 7,143
  • 2
  • 35
  • 41
Robert Louis Murphy
  • 1,558
  • 1
  • 16
  • 29

2 Answers2

3

Try:

RewriteCond %{HTTP_HOST} ^(www\.)?explain([^\.]+)\.com$ [NC]
RewriteCond %{REQUEST_URI}:%2 ^/([^/]+)/?:\1 [NC]
RewriteRule ^ / [R=301,L]

The trick here is to first group the match for anything after the explain and before the .com in the %{HTTP_HOST}, then (since we can't use % variables in the second parameter for a RewriteCond) we put the URI and the previous backreference together, and use a \1 to match against that. If they're the same, redirect to /.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
0

If all you want is for http://www.explainmortgage.com/mortgage to redirect to http://www.explainmortgage.com, the following rule in your .htaccess file will do that. The NC flag makes the comparison case insensitive. This will NOT affect (redirect or rewrite) URI=/mortgage/.

RewriteRule ^mortgage$ / [R=301,L,NC]

From you comment you say that you want to redirect more URLs to the home page, then perhaps the following may help achieve what you want. The following will redirect URLs pointing to non-existant files or directories to your home page on any domain. For example if "/mortgage", "/bob", "/carol" do not exist as files, then they will all be redirected to "/".

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . / [R=301,L]
HeatfanJohn
  • 7,143
  • 2
  • 35
  • 41
  • I know how to do that, I want one set of rules for multiple situations that will work as new domains and categories are added: http://www.explaincars.com/cars, http://www.explaineducation.com/education, http://www.explainelectronics.com/electronics, etc... I don't want to add a line to my .htaccess file for each domain. I'm using an open source codebase and want to avoid editing it as much as possible. – Robert Louis Murphy Aug 01 '12 at 16:04
  • I have another question, in the .htaccess file, if the URL is http://www.explainmortgage.com/mortgage, does {REQUEST_URI} equal mortgage or /mortgage? – Robert Louis Murphy Aug 01 '12 at 16:07
  • Your question contains a misconception. The HTTP_HOST variable only contains the request's host name. It does not contain any URL path information. – HeatfanJohn Aug 01 '12 at 16:19
  • Oh, wait I think I see what you mean, I didn't notice that your host name had mortgage in it. Let me think about this more. I see that all of your Host names contain "explain" followed by some word. – HeatfanJohn Aug 01 '12 at 16:26