How can I hide folder name from URL using .htaccess

╄→尐↘猪︶ㄣ 提交于 2019-12-11 02:07:36

问题


I have a website with the root folder 'www', but I put all php files including index.php in a sub-folder of root. I wrote myself a .htaccess file to redirect, so if I input www.test.com, it will jump to www.test.com/folder and display the index.php.

Below it's my .htaccess which I put in the root.

 RewriteEngine On
 RewriteBase /
 RewriteRule ^folder2 - [L] 
 #ignore folder2 in which I put important files, but no works for the website
 RewriteCond %{REQUEST_URI} !^/folder(.*)
 RewriteRule (.*) /folder/$1

Right now, I want to change the .htaccess file to reach these goals:

  1. When I input www.test.com, jump to www.test.com/folder as usual, but display the url without folder.
  2. All the pages in folder will display the url without folder name. Such as

    www.test.com/shop/page1 -> www.test.com/page1

I searched some of the scripts, but none of them works.


回答1:


I've found this related question which answers to your problem. The snippet from the answer is (note: it's adjusted to your needs):

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^folder/)^(.*)$ /folder/$1 [L,NC]

Reference: https://stackoverflow.com/a/18361995/3673491




回答2:


Based on your current rules. You can just make a slight adjustment.

 RewriteEngine On
 RewriteBase /
 #ignore folder2 in which I put important files, but no works for the website
 RewriteRule ^folder2/? - [L] 

 RewriteRule ^/?$ /folder/ [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ /folder/$1 [L]



回答3:


You can use below code.

RewriteEngine On

RewriteBase /

RewriteRule ^/?$ /folder/ [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ /folder/$1 [L]



来源:https://stackoverflow.com/questions/30194198/how-can-i-hide-folder-name-from-url-using-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!