Kreg Ertman
Kreg Ertman

Reputation: 1

htaccess - redirect to ignore nested subdirectory and if possible make entire site non-www

I have an issue that's too complex for me to handle, but I'm betting someone has had to do this before, so please let me hear from you. ;)

Here's the situation:

I've got 1 main domain with 3 subdirectories that are nested within each other

(from top to bottom) http://main-domain.com then http://main-domain.com/company-name/ then http://main-domain.com/company-name/blog/

There's currently 3 .htaccess files -- 1 in each of the 3 directories shown above.

What's the problem?

Instead of having www.main-domain.com/company-name/blog/whatever, I'd like to have main-domain.com/blog/whatever

So, I want to drop the www AND more importantly, drop the middle subdirectory; i.e. /company-name/

I hope that the following examples will help to illustrate the point.

http://main-domain.com/company-name/index.php should be changed to http://main-domain.com/index.php

http://main-domain.com/company-name/blog/my-first-article/ should be changed to http://main-domain.com/blog/my-first-article/

Why do I need this?

I need a shorter URL that is more SEO-friendly. I have too many backlinks that use the 'old' urls, so I need to mod-rewrite them all.

Here are My Current 3 htaccess files

root htaccess: main-domain.com

#Bypass InoCore Templating System

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /reservations/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /reservations/default.php [L]

Options -Indexes

</IfModule>

#END Bypass

#301 REDIRECT

Options +FollowSymlinks

RewriteEngine on

RewriteRule ^info.php - [L]

RewriteCond %{HTTP_HOST} ^www.domain1.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]

RewriteCond %{HTTP_HOST} ^domain1.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]

RewriteCond %{HTTP_HOST} ^www.domain2.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]

RewriteCond %{HTTP_HOST} ^domain2.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]

RewriteCond %{HTTP_HOST} ^www.domain3.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]

RewriteCond %{HTTP_HOST} ^domain3.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]

RewriteCond %{HTTP_HOST} ^main-domain.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]

RewriteCond %{HTTP_HOST} ^www.main-domain.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]

company-name htaccess: main-domain.com/company-name/

RewriteEngine on

RewriteRule ^maping.php /maping.php

RewriteRule ^$ index.php?$1 [L]

RewriteRule (.*) index.php?$1 [L]

#php_flag magic_quotes_gpc off

#BEGIN WordPress

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /company-name/

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /company-name/index.php [L]

</IfModule>

#END WordPress

blog htaccess: main-domain.com/company-name/blog/

RewriteEngine off

#BEGIN WordPress

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteBase /company-name/blog/

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /company-name/blog/index.php [L]

</IfModule>

#END WordPress

Upvotes: 0

Views: 1523

Answers (1)

anubhava
anubhava

Reputation: 785156

Your correct and compact root .htaccess should be like this:

Options +FollowSymlinks

RewriteEngine on

RewriteRule ^info.php - [L]

# match all the domains in single condition while www. is optional
RewriteCond %{HTTP_HOST} ^(www\.)?(domain1|domain2|domain3|main-domain)\.(com|tld)$ [NC]
RewriteRule ^company-name/(.*)$ http://www.domain.tld/$1 [R=301,L,NC,NE]
  • R=301 will redirect with https status 301
  • L will make last rule
  • NE is for no escaping query string
  • NC is for ignore case comparison
  • $1 is your REQUEST_URI matching group

Upvotes: 1

Related Questions