mod-rewrite将子URL重定向到父URL [英] mod-rewrite redirect child URL to parrent URL
问题描述
在.htaccess中,我想在第二个'/'之后截取任何内容,因此 http: //www.domain.com/parent/child/otherchild 将重定向到 http:// www .domain.com / parent /
in .htaccess I would like to chop off anything after the 2nd '/'… so http://www.domain.com/parent/child/otherchild would be redirected to http://www.domain.com/parent/
推荐答案
将以下规则添加到文档根目录中的htaccess文件中:
Add these rules to the htaccess file in your document root:
RewriteEngine On
RewriteRule ^([^/]+)/.+$ /$1/ [L,R=301]
不确定所有粘贴的意思注释中的代码不可读,但是您想添加我在注释中提到的排除条件:
Not sure what you meant by pasting all that unreadable code in the comments, but you want to add exclusion conditions like I mentioned in my comments:
AddType application/octet-stream vcf
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(css|js)$ [NC]
RewriteRule ^([^/]+)/.+$ /$1/ [L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !^/wp-content/themes/myTheme/style.css
RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
由于!
:
RewriteCond %{REQUEST_URI} !\.(css|js)$ [NC]
这意味着URI是否以 .css $ c结尾$ c>或
.js
,然后不应用规则。您可以根据需要添加其他人。假设您不想重定向URI,例如: / foo / bar / no-redirect /
,然后添加:
That means if the URI ends with .css
or .js
then don't apply the rule. You can add others as needed. Say you don't want to redirect a URI like: /foo/bar/no-redirect/
, then also add:
RewriteCond %{REQUEST_URI} !^/foo/bar/no-redirect/
这篇关于mod-rewrite将子URL重定向到父URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!