如何将斜杠后的所有内容重定向到域之前? [英] How to redirect everything after slash to domain before?

查看:53
本文介绍了如何将斜杠后的所有内容重定向到域之前?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想要做的:

我希望 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆