.htaccess将/ dir重定向到/ index?page = dir

时间:2018-12-07 19:49:58

标签: php regex .htaccess

我希望将example.com/subdir重定向到example.com?page=subdir,同时仍将example.com/subdir保留在URL中。这样,我可以使用不同的get请求动态地“创建页面”。我已经确定了如何执行此操作,但是我似乎无法弄清楚如何防止example.com/subdir/subdir导致资产加载不正确。这是我当前的代码:

Options +FollowSymLinks
RewriteEngine on
# Redirect /page.php, /page.html, and /page.htm to /page
RewriteRule ^(.+)\.(php|html|htm)\/*$ /$1 [R,L]
# If the page is not a file or directory
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-d
# Internally redirect /page to /index.php?page=page
RewriteRule ^(.*?)/?$ /index.php?page=$1 [NC,END]
# Redirect /index to /
Redirect /index /

1 个答案:

答案 0 :(得分:0)

.htaccess中仅保留以下规则:

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index|(\S+?))\.(?:php|html)[/\s?] [NC]
RewriteRule ^ /%1%2 [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)/?$ index.php?page=$1 [L,QSA]

请确保使用新的浏览器进行测试,以避免旧的缓存。