mod-rewrite redirect child URL to parrent URL

落花浮王杯 提交于 2020-01-11 13:41:06

问题


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/


回答1:


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

This line is an exclusion, because of the !:

RewriteCond %{REQUEST_URI} !\.(css|js)$ [NC]

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/



回答2:


Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteRule ^([^/]+/).+$ /$1 [L,R=301]


来源:https://stackoverflow.com/questions/19552661/mod-rewrite-redirect-child-url-to-parrent-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!