簡體   English   中英

.htaccess url用變量重寫

[英].htaccess url rewrite with variables

我正在嘗試通過使用重寫來更改我的網址在我的網站上的特定子目錄中看起來更漂亮:
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]就會停止。

代替:

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

嘗試:

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

你是正確的, [L]會停止處理規則,但這也意味着你不能消除.php擴展名,因為它的規則低於這個規則。 還要注意領先的斜線。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM