【问题标题】:.htaccess url rewrite with variables.htaccess url 用变量重写
【发布时间】:2011-10-19 20:35:10
【问题描述】:

我正在尝试通过使用重写来使我的网址在我网站的特定子目录中看起来更漂亮:
www.example.com/sub/file/{some_number}/ 内部:
www.example.com/sub/file.php?var={some_number}
而且我不断收到 404。 我已经使用如下所示的 .htaccess 文件从我的站点中删除了 .php 扩展名:

RewriteEngine on
#
## Internally rewrite extensionless file requests to .php files ##
#
# If the requested URI does not contain a period in the final path-part
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
# and if it does not exist as a directory
RewriteCond %{REQUEST_FILENAME} !-d
# and if it does not exist as a file
RewriteCond %{REQUEST_FILENAME} !-f
# then add .php to get the actual filename
RewriteRule (.*) /$1.php [L]
#
## Externally redirect clients directly requesting .html page URIs to extensionless URIs
#
# If client request header contains html file extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+\.)+html\ HTTP
# externally redirect to extensionless URI
RewriteRule ^(.+)\.html$ http://www.dirtycoffee.com/$1 [R=301,L]

# If client request header contains php file extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+\.)+php\ HTTP
# externally redirect to extensionless URI
RewriteRule ^(.+)\.php$ http://www.dirtycoffee.com/$1 [R=301,L]

我目前的尝试是在顶部添加一行:

#Subdirectory rewrite:
RewriteRule ^sub/file/([0-9]+)/?$ sub/file?num=$1 [L] # Handle requests for file

我知道我必须有冲突的规则,但我认为如果应用新规则,[L] 会停止。

【问题讨论】:

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


    【解决方案1】:

    代替:

    RewriteRule ^sub/file/([0-9]+)/?$ sub/file?num=$1 [L]
    

    试试:

    RewriteRule ^sub/file/([0-9]+)/?$ /sub/file.php?num=$1 [L]
    

    [L] 将停止处理规则是正确的,但这也意味着您无法消除 .php 扩展名,因为该规则低于此规则。还要注意前面的斜线。

    【讨论】:

    • 做到了!非常感谢!我想我一直盯着这种方式太久了。此外(Google 员工的旁注)必须更新指向我的 css 表的链接,因为该页面将其视为目录更改。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-07
    • 2017-10-01
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多