【问题标题】:remove directory after main url using htaccess使用 htaccess 在主 url 之后删除目录
【发布时间】:2013-05-05 20:17:44
【问题描述】:

使用以下 url http://www.example.com/de/here/ 我想删除“de”目录(或任何可能在“here”目录前面的东西,如果它前面有任何东西的话)所以用户被定向到@987654322 @ 而不是,这是一个确实存在的目录。

网址甚至可以是 http://www.example.com/it/here/ 或任何其他 2 个字母的组合。

网址也可能只是http://www.example.com/here/,在这种情况下,我根本不希望删除任何内容。

我已经在这里搜索了一个解决方案,但似乎无法让任何事情正常工作,因此非常感谢任何帮助。

【问题讨论】:

    标签: .htaccess mod-rewrite rewrite


    【解决方案1】:

    你可以使用这种 htaccess :

    RewriteEngine On
    RewriteBase /
    RewriteRule ^[A-Za-z]{2}/(.*)$ $1 [L,R=301]
    

    此代码引起的重定向示例:

    http://www.example.com/de/foo/  => http://www.example.com/foo/
    http://www.example.com/de/      => http://www.example.com/
    http://www.example.com/it/bar/  => http://www.example.com/bar/
    http://www.example.com/FR/baz/  => http://www.example.com/baz/
    

    请注意,您将无法再访问该语言(de、it、fr...)。

    还有一点,这种url要小心(重定向会执行两次):

    http://www.example.com/de/go/   => http://www.example.com/go/
    http://www.example.com/go/      => http://www.example.com/
    

    编辑

    现在我有了更多详细信息,这是一个 htaccess,您可以删除指定文件夹的语言:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^[A-Za-z]{2}/here/(.*)$          here/$1 [L,R=301]
    RewriteRule ^[A-Za-z]{2}/anotherfolder/(.*)$ anotherfolder/$1 [L,R=301]
    

    【讨论】:

    • 感谢您的回复,问题是就像您说的那样,它会删除所有内容的语言目录,而我只需要在访问特定目录时删除语言目录,这在上面的例子是“这里”。所以不幸的是,这对我不起作用。
    • 你应该从一开始就提到这一点,看我的编辑。
    • 我很抱歉造成混乱......无论如何你的答案现在对我有用。非常感谢zessx
    • 不客气。尝试在使用正则表达式时始终考虑各种异常,视觉方案可以为您提供帮助。
    • 好的,谢谢您的建议。在过去的几个晚上,我一直在做一些关于这个主题的阅读。一开始很混乱,但现在我开始掌握它。再次为您的帮助喝彩。
    猜你喜欢
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-15
    • 2021-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多