【问题标题】:.htaccess redirect /dir to /index?page=dir.htaccess 重定向 /dir 到 /index?page=dir
【发布时间】:2018-12-07 19:49:58
【问题描述】:

我希望将example.com/subdir 重定向到example.com?page=subdir,同时仍将example.com/subdir 保留在URL 中。这样,我可以使用不同的获取请求动态地“创建页面”。我已经知道如何做到这一点,但我似乎无法弄清楚如何防止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 /

【问题讨论】:

标签: php regex .htaccess


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-30
    • 2021-04-29
    • 2014-08-16
    • 2012-06-13
    相关资源
    最近更新 更多