.htaccess: Redirect root url to subdirectory, but retain root url
I am in the process of cleaning up my domain's directory structure (woe be to me for setting up the root url's content in the root directory!) and need some insight on how to use RewriteRule correctly.
The Gist
I want domain.tld to use domain.tld/subdirectory/ but still appear as domain.tld in the url.
My .htaccess So Far
Options +FollowSymlinks
RewriteEngine On
#Force removal of wwww subdomain
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.domain.tld
RewriteRule ^(.*)$ http://domain.tld/$1 [R=301,L]
#Trailing slash
RewriteRule ^/*(.+/)?([^.]*[^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
#Redirect root url to subdirectory
RedirectMatch 301 ^/subdirectory/$ http://domain.tld/
RewriteRule ^/$ /subdirectory/?$ [L]
The RedirectMatch开发者_开发问答 works great. Unfortunately, t seems the last RewriteRule there should work simply enough, but it doesn't. The old content setup in the root directory still appears.
What am I missing here?
Update: Resolved
Simple fix that I am not experienced enough with the .htaccess / apache to be able to explain why.
I had:
RewriteRule ^/$ /subdirectory/?$ [L]
Removing the one slash fixed everything:
RewriteRule ^$ /subdirectory/?$ [L]
So now my question is: why is that?
Try this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/subdirectory/
# REWRITES ALL URLS
# [REPLACE "domain" WITH THE ACTUAL DOMAIN,
# WITHOUT THE TLD (.com, .net, .biz, etc)]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.
# REWRITE ALL THOSE TO subdirectory
RewriteRule ^(.*)$ /subdirectory/$1 [L]
BR Spiros
The part of URI in a RewriteRule
doesn't contain the preceding slash.
So to match http://example.com/
you have to use RewriteRule ^$
where RewriteRule ^/$
would theoretically match http://example.com//
(note double slash).
Additional: ?$
at the end doesn't make any sense, since this would add a querystring with an empty parameter $
.
Finally you have: RewriteRule ^$ /subdirectory/ [L,QSA]
Both rules are VALID:
This one to be used in server configuration or virtual host context (URL starts with leading slash):
RewriteRule ^/$ /subdirectory/ [L]
If you place the same rule in .htaccess, then you have to remove leading slash (because it is relative to the current folder):
RewriteRule ^$ /subdirectory/ [L]
P.S.
Floern already mentioned about ?$
it target URL part. No need for $
at all. If you finish your URL with ?
this tells Apache to not to transfer query string over to new URL (drop query string completely).
Additionally, [QSA]
flag is only required if you manipulating with query string (have ?
in target URL) and still would like your current query string be appended to the newly build URL. This means that in this rule RewriteRule ^$ /subdirectory/ [L,QSA]
the [QSA]
flag does nothing at all and can be safely removed.
i tried the solution but in my case removes the /folder/ BUT showing the old site (www.site.it) and not the www.site.it/folder/ with 'folder' removed... i copied and pasted the same code but doesn't works in my case :( i opened a new topic: .htaccess: Redirect root wordpress url to another wordpress in a subdirectory, but retain root url
精彩评论