【问题标题】:Rewriting urls with Htaccess like filename/param1/param2 etc etc使用 Htaccess 重写 url,如文件名/param1/param2 等
【发布时间】:2013-06-25 13:47:21
【问题描述】:

我正在为以下情况寻找解决方案。我想隐藏以下网址

www.example.com/filename.php?p1=1&p2=2 etc

www.exmaple.com/filename/1/2 etc

我设法用 index.php 做到了这一点,但我也想使用其他文件,而不仅仅是索引。是否可以?这是我当前的代码。

Options +FollowSymlinks -MultiViews

RewriteEngine On
RewriteBase /

# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteRule ^(filename)/([^/]+)/([^/]+)$ /$1.php?p1=$2&p2=$3 [L,QSA,NC]


ErrorDocument 404 /404.php

# display no errs to user
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off

这适用于 php 删除。但是如果我在它之后添加参数,它会给我错误 500

【问题讨论】:

    标签: .htaccess url mod-rewrite rewrite


    【解决方案1】:

    RewriteEngine On 行下方插入此规则:

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^(filename)/([^/]+)/([^/]+)/?$ /$1.php?p1=$2&p2=$3 [L,QSA,NC]
    

    【讨论】:

    • 如果我这样尝试 www.example.com/profile/u/a 会给我 404 错误。 profile 是文件名,u 和 a 是参数
    • 加载时发生了什么:www.example.com/profile.php?p1=u&p2=a
    • 不行,还是不行。 .php 删除可能有一些问题。它给了我 404 错误。
    • 很可能是因为某些规则冲突。您能否再次在您的问题中发布最新的 .htaccess。
    • 为此设置另一个 RewriteRule:RewriteRule ^(profile)/([^/]+)/?$ /$1.php?p1=$2 [L,QSA,NC]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2013-04-22
    • 2017-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多