Solved

.htaccess - Hiding the subdirectory using wordpress

Posted on 2014-03-27
7
518 Views
Last Modified: 2014-03-31
Currently we have the following configuration on our .htaccess file:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /v2/index.php [L]
</IfModule>

# END WordPress

RewriteCond %{HTTP_HOST} ^meadeauto\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.meadeauto\.com$
RewriteRule ^/?$ "http\:\/\/www\.meadeauto\.com\/v2" [R=301,L]
RewriteCond %{HTTP_HOST} ^meadegroup\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.meadegroup\.com$
RewriteRule ^/?$ "http\:\/\/www\.meadeauto\.com" [R=301,L]
RewriteCond %{HTTP_HOST} ^meadelexus\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.meadelexus\.com$
RewriteRule ^/?$ "http\:\/\/www\.meadeauto\.com" [R=301,L]

Open in new window


When you visit www.meadeauto.com it shows www.meadeauto.com/v2 in the browser URL bar. We want it to show (rewrite): www.meadeauto.com in the URL bar.

We have tried various combinations, including this solution we found on a blog post:

RewriteBase /

# Add trailing slash if path does not contain a period or end with a slash
RewriteCond %{REQUEST_URI} !(\.|/$)
RewriteRule (.*) http://www.yoursite.com/$1/ [R=301,L]

#Change http://yoursite.com to http://www.yoursite.com (Optional)
RewriteCond %{HTTP_HOST} ^yoursite.com$
RewriteRule ^/?(.*)$ http://www.yoursite.com/$1 [R=301,L]
#Rewrites http://www.yoursite.com/subdir to http://www.yoursite.com/
RewriteCond %{REQUEST_URI} !^/subdir
RewriteRule ^(.*)$ subdir/$1 [L]

Open in new window


That seemed to change it to meadeauto.com in the url bar, but also broke www.gistroy.com (another domain, which is in the /gis/ folder on our main directory).
0
Comment
Question by:meade470
  • 3
  • 2
  • 2
7 Comments
 
LVL 15

Expert Comment

by:eemit
Comment Utility
Insert this above the line:
# BEGIN WordPress

# protect "dir"
Options All -Indexes

Open in new window

0
 
LVL 2

Author Comment

by:meade470
Comment Utility
eemit, unfortunately that is still showing the subdirectory (www.meadeauto.com/v2/) when loading meadeauto after making the change to:

# protect "dir"
Options All -Indexes

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /v2/index.php [L]
</IfModule>

# END WordPress

RewriteCond %{HTTP_HOST} ^meadeauto\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.meadeauto\.com$
RewriteRule ^/?$ "http\:\/\/www\.meadeauto\.com\/v2" [R=301,L]
RewriteCond %{HTTP_HOST} ^meadegroup\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.meadegroup\.com$
RewriteRule ^/?$ "http\:\/\/www\.meadeauto\.com" [R=301,L]
RewriteCond %{HTTP_HOST} ^meadelexus\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.meadelexus\.com$
RewriteRule ^/?$ "http\:\/\/www\.meadeauto\.com" [R=301,L]

Open in new window

0
 
LVL 50

Accepted Solution

by:
Steve Bink earned 333 total points
Comment Utility
Your first RewriteRule set after the WordPress section is explicitly commanding the redirect you want to avoid.   Because you are also using that set to enforce a canonical domain, it must necessarily be a redirect, not a simple rewrite.   You'll need to separate those concepts:
RewriteCond %{HTTP_HOST} ^meadeauto\.com$ [NC]
RewriteRule ^/?(.*)$ http://www.meadeauto.com/$1 [R=301,L,QSA]
RewriteRule ^/?$ /v2 [L]

Open in new window


The second issue is your report of rewrites bleeding over to other domains.  This is due to directing more than one domain to the same document root.  If you cannot properly separate the domains, then all of your rewrites must be modified to include a check for the domain.  The above set would be modified to:
RewriteCond %{HTTP_HOST} ^meadeauto\.com$ [NC]
RewriteRule ^/?(.*)$ http://www.meadeauto.com/$1 [R=301,L,QSA]
RewriteCond %{HTTP_HOST} ^www\.meadeauto\.com$ [NC]
RewriteRule ^/?$ /v2 [L]

Open in new window

Ideally, separate the domains to have more granular control with less rewrite management.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Author Comment

by:meade470
Comment Utility
routinet, thanks for the thorough response. We changed it to the following:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /v2/index.php [L]
</IfModule>

# END WordPress

RewriteCond %{HTTP_HOST} ^meadeauto\.com$ [NC]
RewriteRule ^/?(.*)$ http://www.meadeauto.com/$1 [R=301,L,QSA]
RewriteCond %{HTTP_HOST} ^www\.meadeauto\.com$ [NC]
RewriteRule ^/?$ /v2 [L]
RewriteCond %{HTTP_HOST} ^meadegroup\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.meadegroup\.com$
RewriteRule ^/?$ "http\:\/\/www\.meadeauto\.com" [R=301,L]
RewriteCond %{HTTP_HOST} ^meadelexus\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.meadelexus\.com$
RewriteRule ^/?$ "http\:\/\/www\.meadeauto\.com" [R=301,L]

Open in new window


but unfortunately were still getting the same results. Do you have any further recommendations/changes?
0
 
LVL 2

Author Comment

by:meade470
Comment Utility
We completely re-wrote it and it now contains:

# The below settings will forward each incoming domain name to the respective
# folders. gis/ is gistroy.com and v2/ is meadeauto.com WP install
# RewriteEngine On
# RewriteCond %{HTTP_HOST} ^(www.)?domainname.com$
# RewriteRule ^(/)?$ subdir/index.php [L]

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?gistroy.com$
RewriteRule ^(/)?$ gis/index.php [L]

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?meadeauto.com$
RewriteRule ^(/)?$ v2/index.php [L]

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?meadelexus.com$
RewriteRule ^(/)?$ v2/index.php [L]

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?meadegroup.com$
RewriteRule ^(/)?$ v2/index.php [L]

# Added the below to prevent htaccess viewing and directory listings

<Files .htaccess>
order allow,deny
deny from all
</Files>

Options All -Indexes

 

Open in new window


It seems to work now! We used this resource from WP for it.

Any additional suggestions to spruce it up is appreciated.
0
 
LVL 15

Assisted Solution

by:eemit
eemit earned 167 total points
Comment Utility
Not sure, but check if you also need Non-www to www, 301 redirect:

# BEGIN Non-www to www
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^YourDomain.com
RewriteRule (.*) http://www.YourDomain.com/$1 [R=301,L]
# END Non-www to www

Open in new window

0
 
LVL 50

Assisted Solution

by:Steve Bink
Steve Bink earned 333 total points
Comment Utility
What you have now looks fine, though give consideration to eemit's comment regarding enforcing the canonical "www" domain.  

The complexity of your rewrites are necessary because of the combined document root.  Likewise, remember that any rewrite set you include will act on *all* domains pointing to that root.  In order to isolate behavior on just one of the sites, you will need to include the RewriteCond check for the host name before each RewriteRule.
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
The purpose of this video is to demonstrate how to reset a WordPress password if you are locked out and cannot reset the password. A typical use would be if you cannot access the email to which WordPress would send the password recovery email to…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now