【问题标题】:Same page with 2 urls .htaccess no redirect具有 2 个 url 的同一页面 .htaccess 无重定向
【发布时间】:2016-09-05 10:49:33
【问题描述】:

我有一个列出我所有产品的页面。但我也想要不同网址上的同一页面。所以这两个 url 应该使用任何重定向打开同一个页面。

原始网址为:http://example.com/product/all-products 内容相同的新网址应为http://example.com/all-products

如何使用 .htaccess 做到这一点?

【问题讨论】:

  • 您要重定向到的原始网址是什么意思?
  • @Abhishekgurjar 原始网址意味着我正在运行一个页面,其中列出了我的所有产品。我只想创建另一个网址来访问同一页面而无需任何重定向

标签: php .htaccess


【解决方案1】:

试试下面的规则

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([\w-]+)$ product/$1 [L]

【讨论】:

  • 什么将代码重定向到 abc.com/product/products
  • 可以尝试自己做一些事情@mohit。这是一个问答网站,不是免费的开发者工作网站
  • 实际上这就是我对原始 url 的意思 abc.com/product/products 是重写到哪个 url 你理解我喜欢 product.php?name=products
【解决方案2】:

您可以在 /root.htaccess 中使用以下内容:

RewriteEngine on
#1 redirect browser url from "/product/foobar" to "/foobar"
RewriteCond %{THE_REQUEST} /product/(.+)\sHTTP [NC]
RewriteRule ^ /%1 [L,R,NE]
#2 if "/foobar" exists as a file or dir in product folder
RewriteCond %{DOCUMENT_ROOT}/product/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/product/$1 -d
#3 then rewrite "/foobar" to "/product/foobar"
RewriteRule ^(.*?)/?$ /product/$1 [L]

如果上面的例子失败了,你可以试试下面的

RewriteEngine on

RewriteCond %{THE_REQUEST} /product/(.+)\sHTTP [NC]
RewriteRule ^ /%1 [L,R,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /product/$1 [L]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 2013-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多