mod_rewrite RewriteRule

时间:2011-04-27 13:42:23

标签: apache .htaccess url mod-rewrite url-rewriting

我确定这很简单,但我完全不能让它工作:\ 我正在尝试改写

http://www.example.com/products/ABC123

http://www.example.com/index.php?page=shop&code=ABC123

我试过了

RewriteBase /

RewriteRule ^products/([A-Za-z0-9]+)$ index.php?page=shop&code=$1

哪个重定向好,但所有图像(和js css等)都有错误的路径(example.com/images/ABC123.jpg是example.com/products/images/ABC123.jpg)。此外,所有链接现在都是错误的路径(example.com/?page=shop&folder=7是example.com/products/?page=shop&folder=7)

我的.htaccess文件在根目录中,我也尝试过RewriteBase产品/并从规则正则表达式中删除产品/但只是抛出404

我在过去一小时内多次阅读过有关mod_rewrite的官方文档,我一定错过了什么?

**编辑:** 对不起,所有这都是一个棘手的问题,事实证明你可以使用html BASE元素来解决这个问题,我的正则表达式在开始时没有任何问题!

我把< base href =“http://www.example.com/”/>在HEAD部分,现在一切都很好,花花公子!

感谢那些向我展示如何设置天气条件的人们,文件/目录是否存在

2 个答案:

答案 0 :(得分:1)

在.htaccess文件中尝试此规则:

Options +FollowSymlinks -MultiViews
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^products/([-a-z0-9]+)$ /index.php?page=shop&code=$1 [L,NC,QSA]

答案 1 :(得分:0)

      RewriteEngine On
      RewriteBase /

      # Generic requests for the application
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^products/(.*)$ index.php?page=shop&code=$1 [L]

那是吗?

编辑:啊,好吧,你的页面内链接错误是你应该在PHP中解决的问题。你可以试试这个:

    RewriteEngine On
    RewriteBase /

    # Generic requests for the application
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # Main page
    RewriteRule ^products/([A-Za-z0-9]+)$ index.php?page=shop&code=$1 [L]
    RewriteRule ^products/?(.*)$ index.php?$1 [L]

    # Resources
    RewriteRule ^products/images/(.*)$ images/$1 [L]
    RewriteRule ^products/css/(.*)$ css/$1 [L]
    RewriteRule ^products/js/(.*)$ js/$1 [L]

但实际上,你应该在php中生成你的URL,这样它们才能正常工作:每次都必须通过mod_rewrite运行它们。

相关问题