.htaccess是否将/dir重定向到/index?page=目录



我希望将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 /

在.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]

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

相关内容

最新更新