的.htaccess URL重写与变数 [英] .htaccess url rewrite with variables

查看:118
本文介绍了的.htaccess URL重写与变数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的网址看起来是prettier在我的网站上某个特定子目录通过重写改变:
     www.example.com/sub/file/ {some_number} / 在内部:
     www.example.com/sub/file.php?var= {some_number}
我不断收到一个404。 我已经从我的网站使用看起来像这样一个.htaccess文件中删除PHP扩展:

I'm trying to make my urls look prettier in a specific subdirectory on my site by using rewrite to change:
www.example.com/sub/file/{some_number}/ internally to:
www.example.com/sub/file.php?var={some_number}
And I keep getting a 404. I'm already removing the .php extension from my site using an .htaccess file that looks like this:

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]

我现在的尝试是添加一行在像顶部:

My current attempt is to add a line at the top like:

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

我知道我必须有冲突的规则,但我认为[L],如果新规则应用于只想停下来。

I know I must have conflicting rules, but I thought [L] would just stop if the new rule was applied.

推荐答案

相反的:

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

尝试:

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

你是正确的 [L] 将停止处理规则,但是这也意味着你不能消除.php扩展,因为该规则是下方这个。另外要注意的斜线。

You're correct that [L] will stop processing rules, but that also means that you can't eliminate the .php extension since the rule for that is below this one. Also note the leading slash.

这篇关于的.htaccess URL重写与变数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆