.htaccess: redirect the root url to a subdirectory but keep the root url

I am cleaning up my domain directory structure (woe to me for setting the root URL in the root directory!) And you need to understand how to use the RewriteRule correctly.

The gist

I want domain.tld to use domain.tld / subdirectory /, but still display 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] 

RedirectMatch works great. Unfortunately, it seems that the last RewriteRule should work quite simply, but it is not. The old content setting in the root directory still appears.

What am I missing here?

Update: Allowed

A simple fix is ​​that I am not experienced with .htaccess / apache enough to explain why.

I have had:

 RewriteRule ^/$ /subdirectory/?$ [L] 

Removing one slash fixed everything:

 RewriteRule ^$ /subdirectory/?$ [L] 

So now my question is: why is this?

+6
source share
4 answers

Try the following:

 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 Spyros

+1
source

The URI part in the RewriteRule does not contain the previous slash.

So, to match http://example.com/ , you should use RewriteRule ^$ , where RewriteRule ^/$ theoretically matches http://example.com// (note the double slash).

Additionally ?$ At the end does not make any sense, as this will add a query with an empty parameter $ .

Finally, you have: RewriteRule ^$ /subdirectory/ [L,QSA]

0
source

Both rules: VALID :

This one is used to configure the server or virtual host context (URL begins with a leading slash):

 RewriteRule ^/$ /subdirectory/ [L] 

If you put the same rule in .htaccess , you need to remove the leading slash (because it refers to the current folder):

 RewriteRule ^$ /subdirectory/ [L] 

PS The ?$ Destination URL has already been mentioned. No need for $ at all. Should you end your url with ? this tells Apache not to pass the query string to the new URL (completely exclude the query string).

In addition, the [QSA] flag is only required if you are manipulating the query string (is there ? In the destination URL) and want your current query string to be added to the new URL. This means that in this RewriteRule ^$ /subdirectory/ [L,QSA] rule, the RewriteRule ^$ /subdirectory/ [L,QSA] flag does nothing and can be safely removed.

0
source

I tried the solution, but in my case it deletes / folder / BUT, showing the old site (www.site.it) and not www.site.it/folder/ with the deleted folder ... I copied and pasted the same code but it doesn’t work in my case :( I opened a new topic: .htaccess: Redirect root wordpress url to another wordpress in a subdirectory, but keep the root url

0
source

Source: https://habr.com/ru/post/893252/


All Articles