使用 .htaccess 隐藏 url 中的子目录

问题描述 投票:0回答:1

我有以下目录结构

Root(www.example.com)/
                  index.php(simple page with links pointing to subdirectory like www.example.com/subdirectory/index.php?id=1)
                  subdirectory/
                              index.php
                              .htaccess(handling all subdirectory rewriting and working fine)

所以我当前的网址是

www.example.com/subdirectory/10/

所以我想从网址中隐藏“子目录”,这样最终的网址应该是这样的

www.example.com/10/

我尝试在我的子目录/.htaccess 文件中添加以下规则,但它不起作用

RewriteCond %{REQUEST_URI} !(.*)subdirectory
RewriteRule ^(.*)$ subdirectory/$1 [L]

RewriteRule ^$ subdirectory/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ subdirectory/$1

我的子目录/.htaccess 文件

RewriteEngine On                                                                                                                              

RewriteRule ^([-]?[0-9]+)([-_][^/]*)?/posts/([0-9]+)([-_][^/]*)?/([0-9]+)([-_][^/]*)?/([0-9]+)([-_][^/]*)?\.html  index.php?view=showad&adid=$7&cityid=$1 [QSA]
RewriteRule ^([-]?[0-9]+)([-_][^/]*)?/posts/([0-9]+)([-_][^/]*)?/([0-9]+)([-_][^/]*)?/page([0-9]*)\.html   index.php?view=ads&catid=$3&subcatid=$5&cityid=$1&page=$7 [QSA]
RewriteRule ^([-]?[0-9]+)([-_][^/]*)?/posts/([0-9]+)([-_][^/]*)?/([0-9]+)([-_][^/]*)?     index.php?view=ads&catid=$3&subcatid=$5&cityid=$1 [QSA]
apache .htaccess mod-rewrite
1个回答
0
投票

在您的

subdirectory/.htaccess
中,插入以下规则:

RewriteEngine On
RewriteBase /subdirectory/

# external redirect from actual URL to a pretty one
RewriteCond %{THE_REQUEST} /subdirectory/(\S*)\s [NC]
RewriteRule ^ %1? [R=301,L,NE]

# remaining rules go below this

您还需要在站点的根 .htaccess 中添加这些规则(如果不存在,则创建它):

RewriteEngine On

RewriteRule ^$ subdirectory/ [L]

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ subdirectory/$1 [L]
© www.soinside.com 2019 - 2024. All rights reserved.