问题
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