【问题标题】:htacces and mod rewrite with second parameter and existing folders [closed]htaccess 和 mod_rewrite 与第二个参数和现有文件夹 [关闭]
【发布时间】:2013-11-10 10:57:36
【问题描述】:

我相信这个问题已经回答了很多次,但我就是找不到答案。

我正在尝试让友好的 url 工作,但无法编写重写规则。

我需要将 www.mysite.com/123 重写为 www.mysite.com/index.php?page=123

还有一个页面,例如:www.mysite.com/123/abcwww.mysite.com/index.php?page=123&secondParam=abc p>

我还希望能够通过 www.mysite.com/something 链接到如下页面:www.mysite.com/something/index.php,而不是将其重写为 www.mysite.com/index.php? page=something 如果文件夹 something 在服务器上存在。

谢谢。

【问题讨论】:

    标签: .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    试试这个。 以“#”开头的行是 cmets,仅用于记录命令。

    <IfModule mod_rewrite.c>
    # calls to initiate our requests Apache's Rewrite Module (you need this only once)
    RewriteEngine on
    
    # "If the requests does not point to an existing file"
    RewriteCond %{REQUEST_FILENAME} !-f
    # "If the requests does not point to an existing folder"
    RewriteCond %{REQUEST_FILENAME} !-d
    # "If the request contains a "2 subfolders" structure, redirects it silently to ..."
    RewriteRule ^([^/]+)/([^/]+)/?$ index.php?page=$1&secondParam=$2 [NC,QSA,L]
    RewriteRule ^([^/]+)/?$ index.php?page=$1 [NC,QSA,L]
    </IfModule>
    

    试试这个版本的最后一行:

    RewriteRule ^(.*)/(.*)/?$ index.php?page=$1&secondParam=$2 [NC,QSA,L]
    

    RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?page=$1&secondParam=$2 [NC,QSA,L]
    

    【讨论】:

    • 这给了我一个内部服务器错误
    • 你确定 mod_rewrite 可用吗?删除这些行,只保留 RewriteEngine,保存并刷新。
    • 我确定。这有效: RewriteRule 上的 RewriteEngine (.*)\.html$ index.php?page=$1
    • so:尝试从底部开始删除每一行,直到您没有内部服务器错误。报告导致问题的行。
    • 注释掉了 之前的最后一条重写规则。内部服务器错误消失了,但现在当我尝试 www.mysite.com/1234 时,出现 404 错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-20
    相关资源
    最近更新 更多