【问题标题】:mod_rewrite: Help to rewrite an URLmod_rewrite:帮助重写 URL
【发布时间】:2013-10-08 03:05:51
【问题描述】:

我有一个网站,我正在尝试重写它的网址。

这很简单,但我不明白怎么做。

这些页面是下一个:

http://localhost/site/index.php?p=home等home是一个参数,重写应该是这样的:http://localhost/site /home.html

http://localhost/site/section.php?id=someid 我希望它像 http://localhost/site/the-name-of-the -section-from-database-SOMEID

http://localhost/site/product.php?id=someid 我希望它像 http://localhost/site/category-name/product-name -SOMEID

我认为这很简单,但我仍然不明白该怎么做。请帮帮我。

谢谢!

【问题讨论】:

  • 我可能错了,但是你不能在本地使用modrewrite。好吧,反正我不能,如果我被证明是错的,我很想知道怎么做!
  • 可以在本地使用它,我有一个启用了 mod_rewrite 的站点,它可以工作。
  • 好的,那有什么问题呢?你没有提供任何规则?

标签: php regex apache .htaccess mod-rewrite


【解决方案1】:

将这些行放在您的 ROOT .htaccess 中:

Options +FollowSymLinks -MultiViews
RewriteEngine on

RewriteRule ^site/(home)\.html/?$ site/index.php?p=$1 [L,QSA,NC]

# Assuming hyphen is not in the section name
RewriteRule ^site/.*-(.*)/?$ site/section.php?id=$1 [L,QSA,NC]

# Assuming hyphen is not in the product name
RewriteRule ^site/[^/]*/.*-(.*)/?$ site/product.php?id=$1 [L,QSA,NC]

【讨论】:

    【解决方案2】:

    这个页面应该可以帮助你.htaccess Url Rewrite Generator。它会为您生成 htaccess 文件。

    【讨论】:

      【解决方案3】:

      http://net.tutsplus.com/tutorials/other/a-deeper-look-at-mod_rewrite-for-apache/ 使用这个链接有一个关于 mod_rewrite 的详细解释。我也认为 anubhava 得到了正确的正则表达式

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-23
        • 1970-01-01
        • 2011-01-27
        • 1970-01-01
        相关资源
        最近更新 更多