具体.htaccess重定向中间带有通配符

时间:2022-09-01 23:25:46

I will preface this by saying that other posts on similar subject matter were not able help me solve this exact issue.

我将在序言中说,关于类似主题的其他帖子无法帮助我解决这个问题。

I am trying to rewrite the following:

Original
mydomain.com/brand-collection/{wildcard1}/{wildcard2}.html

Rewritten to:
mydomain.com/{wildcard2}.html

我正在尝试重写以下内容:原文mydomain.com/brand-collection/{wildcard1}/{wildcard2}.html重写为:mydomain.com/{wildcard2}.html

Example #1:
mydomain.com/brand-collection/sony/sony-xperia.html"
Would be rewritten to:
mydomain.com/sony-xperia.html

Example #2:
mydomain.com/brand-collection/sharp/sharp-aquos.html"
Would be rewritten to:
mydomain.com/sharp-aquos.html

示例#1:mydomain.com/brand-collection/sony/sony-xperia.html“将被重写为:mydomain.com/sony-xperia.html示例#2:mydomain.com/brand-collection/sharp/sharp- aquos.html“将被重写为:mydomain.com/sharp-aquos.html

Any help is very very appreciated. Thanks!

非常感谢任何帮助。谢谢!

1 个解决方案

#1


1  

If you want to "rewrite" then that means the browser is unaffected and the URI is changed completely on the server's end. You'd want something like this in the htaccess file in your document root:

如果你想“重写”那么这意味着浏览器不受影响,并且URI在服务器端完全改变。你需要在你的文档根目录中的htaccess文件中这样的东西:

RewriteEngine on
RewriteRule ^brand-collection/[^/]+/([^/.]+)\.html$ /$1.html [L]

So if the browser requests:

因此,如果浏览器请求:

http://mydomain.com/brand-collection/sony/sony-xperia.html

it gets served the file that is here:

它被提供给这里的文件:

http://mydomain.com/sony-xperia.html

If there is no /sony-xperia.html file, then you'll just get a 404.

如果没有/sony-xperia.html文件,那么您将获得404。

If you want to redirect the browser, just add the R flag to the square brackets.

如果要重定向浏览器,只需将R标志添加到方括号中。

#1


1  

If you want to "rewrite" then that means the browser is unaffected and the URI is changed completely on the server's end. You'd want something like this in the htaccess file in your document root:

如果你想“重写”那么这意味着浏览器不受影响,并且URI在服务器端完全改变。你需要在你的文档根目录中的htaccess文件中这样的东西:

RewriteEngine on
RewriteRule ^brand-collection/[^/]+/([^/.]+)\.html$ /$1.html [L]

So if the browser requests:

因此,如果浏览器请求:

http://mydomain.com/brand-collection/sony/sony-xperia.html

it gets served the file that is here:

它被提供给这里的文件:

http://mydomain.com/sony-xperia.html

If there is no /sony-xperia.html file, then you'll just get a 404.

如果没有/sony-xperia.html文件,那么您将获得404。

If you want to redirect the browser, just add the R flag to the square brackets.

如果要重定向浏览器,只需将R标志添加到方括号中。