如何将目录中的所有 url 重定向到同一 url 的哈希版本



这就是我想要实现的目标。

用户单击链接或在地址栏中键入类似http://test.com/projects/a-project

页面被重定向到http://test.com/projects/#a-project

项目

子目录中存在多个项目。

这是我到目前为止的访问

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule /?projects/(.*)$ /projects/#$1 [L,R,NE]
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

这在 http://htaccess.madewithlove.be/上测试时工作正常,但在我的网站上会导致重定向循环。

有什么想法吗?

你有一个无限循环,因为htaccess看不到hashtag(它只是客户端)。
所以你的规则一次又一次地执行。

相反,您可以使用此代码

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{THE_REQUEST} s/projects/([^s]+)s [NC]
RewriteRule ^ /projects/#%1 [R,L,NE]
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

注意:{THE_REQUEST}用于确保请求不是来自内部重定向,而是来自原始请求(客户端请求)。因此,在此代码中,仅当它来自客户端请求时,它才会重定向到 #(而不是之后,这会导致无限循环)。


编辑:解决你的ajax问题

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{THE_REQUEST} s/projects/([^s]+)s [NC]
RewriteCond %{QUERY_STRING} !^ajax=true$ [NC]
RewriteRule ^ /projects/#%1 [R,L,NE]
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

阿贾克斯呼叫:http://test.com/projects/a-project?ajax=true
正常(用户)呼叫:http://test.com/projects/a-project(重定向到等效#

最新更新