.htaccess hide subdirectory url after redirect

2019-02-18 13:32发布

问题:

I'm redirecting a website to a subdirectory I created. The subdirectory contains an installation of WordPress and now I want to mask the subdirectory url from the website.

Now it's something like http://mywebsite.com/apt/

I want to remove the /apt/ from the URL.

I tried everything today, searched on Google, tried to write various things on the htaccess I found here but nothing works.

On the htaccess on the main directory of the site, I use this code to redirect

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

And on the one in the /apt/ folder I have

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

</IfModule>

# END WordPress

Could someone suggest me how can I hide the /apt/ extension from the URL? Thanks a lot.

回答1:

Try with this: remove the redirect part then use this insead:

RewriteCond %{THE_REQUEST} ^GET\ /apt/
RewriteCond %{HTTP_HOST} ^(www\.)?mysite.com$
RewriteRule ^apt/(.*) /$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?mysite.com$
RewriteRule !^apt/ apt%{REQUEST_URI} [L]

then use the same code for the main website.



回答2:

If you want to remove /apt from url why do you redirect "/" to "/apt"? Try this:

RewriteRule !^apt/ /apt%{REQUEST_URI} [L,NC]

instead of

RewriteRule ^/?$ "http\:\/\/www\.mywebsite\.com/\apt" [R=301,L]

With this rewrite you should see your application in /apt folder under url www.mywebsite.com



回答3:

Create a .htaccess file in root folder, and put this content inside (just change example.com and my_subdir):

 <IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteCond %{HTTP_HOST} ^(www.)?example.com$
      RewriteCond %{REQUEST_URI} !^/my_subdir/
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^(.*)$ /my_subdir/$1
      RewriteCond %{HTTP_HOST} ^(www.)?example.com$
      RewriteRule ^(/)?$ my_subdir/index.php [L] 
    </IfModule>