在我的网站上,我只在我的URL路径中使用了3个斜杠:
https://example.com/this/isatest/现在,我使用.htaccess,它可以(作为副作用)在URL上添加尽可能多的内容:
https://example.com/this/isatest/hipperdihopperdus/pizza/bacon/with/cheese我想自动删除"isatest“之后的所有内容,同时使用.htaccess保持尾随斜杠。
这就是我的.htaccess当前的样子:
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
# 301 Redirect all requests that don't contain a dot or trailing slash to
# include a trailing slash
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]
RewriteCond %{THE_REQUEST} /index\.html [NC]
RewriteRule ^index\.html$ /? [R=301,L,NC]
RewriteRule ^listen/$ /console/ [NC,L]
# Rewrites urls in the form of /parent/child/
# but only rewrites if the requested URL is not a file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]我怎样才能做到这一点?
发布于 2019-05-30 21:04:34
作为第一条规则,在RewriteEngine指令之后,您可以执行如下操作:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+/[^/]+/). /$1 [R=302,L]这将检查在两个路径段和一个斜杠之后是否还有其他内容(点),并重定向到删除“任何其他内容”。
注意,这是302 (临时)重定向。只需将此更改为301 (永久)重定向--如果这是意图的话--一旦您确认它工作正常。这是为了避免浏览器缓存错误重定向,同时测试。
更新:简单地避免重定向以已识别的文件扩展名结束的文件可能更有效。或者可能排除静态资源的已知目录位置。例如:
RewriteCond %{REQUEST_URI} !\.(css|js|jpg|png|gif)$ [NC]
RewriteRule ^([^/]+/[^/]+/). /$1 [R=302,L]或,
RewriteCond %{REQUEST_URI} !^/static-resources/
RewriteRule ^([^/]+/[^/]+/). /$1 [R=302,L]发布于 2019-05-30 21:04:35
您可以将此规则添加到 line下面。
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+/[^/]+/).+$ /$1 [R=301,L,NE]https://stackoverflow.com/questions/56385454
复制相似问题