用变量重写.htaccess url


.htaccess url rewrite with variables

我想让我的url在我的网站上的特定子目录中看起来更漂亮,通过使用重写来改变:
www.example.com/sub/file/{some_number}/内部to:
www.example.com/sub/file.php?var={some_number}
我总是得到404。我已经从我的网站上删除了。php扩展名,使用。htaccess文件,看起来像这样:

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]就会停止。

而不是:

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

试题:

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

你是正确的,[L]将停止处理规则,但这也意味着你不能消除。php扩展,因为该规则低于此规则。还要注意前面的斜杠