如何将斜杠后的所有内容重定向到域之前? [英] How to redirect everything after slash to domain before?
问题描述
这就是我想要做的:
我希望 example.com/car/{whatever}
重定向到 example.com/{whatever}
.
I want example.com/car/{whatever}
redirected to example.com/{whatever}
.
例如,example.com/car/honda
应该重定向到 example.com/honda
For example, example.com/car/honda
should be redirected to example.com/honda
但是如果 /car/
之后没有任何东西,那么就保持原样.
BUT if there is nothing after /car/
, then just leave it as that.
我如何在 .htaccess 中做到这一点?
How can i do that in .htaccess ?
推荐答案
从 URL 中删除前缀非常简单,只需
Removing a prefix from a URL is pretty simple, just
RewriteRule ^car/(.*)$ /$1 [R,L]
为了保留特殊情况,必须放在前面
In order to keep a special case, you must put it in front
RewriteRule ^car/$ - [L]
这条规则说,如果只有 car/
不用管它,请参阅 重写规则替换
This rule says, if there is just car/
leave it alone, see RewriteRule substitution
-(破折号)
破折号表示不应执行替换(现有路径不受影响地通过).当需要在不更改路径的情况下应用标志(见下文)时使用.
- (dash)
A dash indicates that no substitution should be performed (the existing path is passed through untouched). This is used when a flag (see below) needs to be applied without changing the path.
这篇关于如何将斜杠后的所有内容重定向到域之前?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!