【问题标题】:Problems with clean URLs .htaccess - page not found干净 URL 的问题 .htaccess - 找不到页面
【发布时间】:2011-10-05 16:18:16
【问题描述】:

我正在尝试使用 php 制作干净的 url,但我遇到了一些错误。希望有人能帮忙。

我的.htaccess如下:

# Use PHP5 Single php.ini as default
AddHandler application/x-httpd-php5s .php
# this is the initialization
# For security reasons, Option followsymlinks cannot be overridden.
#Options         +FollowSymLinks
Options +SymLinksIfOwnerMatch
RewriteEngine   On
RewriteBase     /
# these are the rewrite conditions
RewriteCond     %{REQUEST_FILENAME}     !-f
RewriteCond     %{REQUEST_FILENAME}     !-d
# and finally, the rewrite rules
RewriteRule     ^([a-zA-Z0-9\-]+)/?$    /price/index.php?var1=$1 [L,QSA]
RewriteRule     ^([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/?$   /price/index.php?var1=$1&var2=$2 [L,QSA]
RewriteRule     ^([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/?$    /price/index.php?var1=$1&var2=$2&var3=$3 [L,QSA]

实际上我正在尝试在子文件夹price/ 中创建干净的网址。

当我输入时:mydomain.com/price/param1 它运行良好,实际上将我重定向到 mydomain.com/price/index.php?var1=param1

但是当我尝试使用更多变量继续前进时,我遇到了问题。当我尝试访问 mydomain.com/price/param1/param2 时,我没有被重定向并且出现 404 错误。

有什么想法吗?提前致谢。

【问题讨论】:

  • 此 .htaccess 位于何处 - 在网站根文件夹中还是 /price/.htaccess ?
  • 这两个RewriteCond 仅适用于紧随其后的单个规则。
  • .htacces 位于根文件夹中,但这不是问题。 mootinator,这是什么意思?
  • @Dan Stern:这意味着您拥有上面的 .htaccess 代码的方式,如果您访问 /price/var1/var2 并且有一些目录与您的 url 的层次结构相同,那么重写就会开始,而且您将无法访问目录。这就是条件存在的原因,!-f!-d 的意思是,仅当请求所在的位置不存在文件或目录时才重写。

标签: php .htaccess http-status-code-404 clean-urls


【解决方案1】:

试试:

# Use PHP5 Single php.ini as default
AddHandler application/x-httpd-php5s .php

<IfModule mod_rewrite.c>
    # This is the initialization
    # For security reasons, Option followsymlinks cannot be overridden.
    #Options +FollowSymLinks
    Options +SymLinksIfOwnerMatch
    RewriteEngine On
    RewriteBase /

    # /price/var1/var2/var3
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^price/([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/?$ /price/index.php?var1=$1&var2=$2&var3=$3 [L,QSA]

    # /price/var1/var2
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^price/([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/?$ /price/index.php?var1=$1&var2=$2 [L,QSA]

    # /price/var1
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^price/([a-zA-Z0-9\-]+)/?$ /price/index.php?var1=$1 [L,QSA]
</IfModule>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-10
    相关资源
    最近更新 更多