简体   繁体   English

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

[英].htaccess redirect /dir to /index?page=dir

I'm looking to redirect example.com/subdir to example.com?page=subdir , while still keeping example.com/subdir in the URL. 我希望将example.com/subdir重定向到example.com?page=subdir ,同时仍将example.com/subdir保留在URL中。 This way, I can dynamically "create pages" with different get requests. 这样,我可以使用不同的get请求动态地“创建页面”。 I've gotten down how to do this, but I can't seem to figure out how to prevent example.com/subdir/subdir from causing assets to load improperly. 我已经确定了如何执行此操作,但是似乎无法弄清楚如何防止example.com/subdir/subdir导致资产加载不正确。 Here's my current code: 这是我当前的代码:

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 /

Keep only these rules in your .htaccess: 在您的.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]

Make sure to use a new browser for your testing to avoid old cache. 确保使用新的浏览器进行测试,以避免旧的缓存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM