【问题标题】:.htaccess Pretty URL rewrite like folder.htaccess 漂亮的 URL 像文件夹一样重写
【发布时间】:2015-12-07 19:55:17
【问题描述】:

我正在尝试将我的一个网址重写为漂亮的网址,但我似乎无法弄清楚。这是我迄今为止尝试过的。

我的文件夹布局

-index.php
-.htaccess
-/product
    -category.php
    -index.php
    -product.php

我的 htaccess 文件

<IfModule mod_rewrite.c>  
RewriteEngine On
RewriteRule ^([^/]+)/?$ category.php?category_id=$1 [L,QSA]
</IfModule>

我想转这个网址

http://example.com/product/category.php?category_id=1&category=car-and-buses

进入这个网址

http://example.com/product/1/car-and-buses

到目前为止,我尝试的所有操作都会在我的浏览器上出现配置错误或 401..

【问题讨论】:

  • 你的 .htaccess 是否在 product 目录中?

标签: regex .htaccess mod-rewrite


【解决方案1】:

我猜你想转身

http://example.com/product/1/car-and-buses

这样您就可以使用 $_GET['category_id'] 获取 category.php 中的参数

http://example.com/product/category.php?category_id=1&category=car-and-buses

试试这个并将 .htaccess 文件放在你的根目录中

<IfModule mod_rewrite.c>
  RewriteEngine On      
  RewriteRule ^product/(.*)/(.*) /product/category.php?category_id=$1&category=$2 [QSA,L]
</IfModule>

编辑!

试试这个,因为上面的解决方案不适合你。这应该将所有请求重写为您的 index.php。这不是您问题的解决方案,但请尝试这样做以确保您的 .htaccess 文件没有任何其他问题

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /index.php?page=$1 [NC,L,QSA]

【讨论】:

  • 可以直接访问页面吗? example.com/product/…
  • 在 RewriteUrl 中的产品前添加一个斜杠,看我编辑的答案。如果它仍然不起作用,请尝试将 R 添加到您的标志 [R,QSA,L] 并确定您最终的位置
  • 我在本地主机上使用它,我应该在服务器上试试吗??
  • 是的,您可以尝试一下,但是当您将 R 添加到标志时,您来到了哪个 URL?您的 .htaccess 在根目录中吗?你的服务器上启用了 mod_rewrite 吗?
  • 是的,mod_rewrite 现已启用。但仍然没有工作你有什么建议要这样做
猜你喜欢
  • 2015-10-14
  • 2016-01-25
  • 1970-01-01
  • 2012-03-04
  • 1970-01-01
  • 1970-01-01
  • 2012-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多