我有這個.htaccess檔案,但我不知道 mod_rewrite,
RewriteEngine On
RewriteRule ^(. [^/])/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/([^.] )\.php [NC]
RewriteRule ^ /%1 [NE,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/?$ /$1.php [L]
我想要實作的是擁有localhost/viewticket/${id}而不是localhost/viewticket.php?id=123
我嘗試了許多.htaccess規則,但除了隱藏.php在我的 URL 中的這條規則之外,沒有一條有效。
uj5u.com熱心網友回復:
我為你創建了這個,只需洗掉你的并復制這個享受^^
IndexIgnore */*
Options FollowSymLinks
AddDefaultCharset utf-8
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^viewticket/([^/] )/?([^/]*)/?([^/]*)$ viewticket.php?id=$1¶m2=$2¶m3=$3 [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/?$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . index.php [L]
</IfModule>```
uj5u.com熱心網友回復:
確保您已更改內部 URL,以便鏈接到/viewticket/<id>. .htaccess然后在根檔案的頂部添加以下內容:
# Prevent MutliViews rewriting "viewticket" and omitting the URL parameter
Options -MultiViews
RewriteEngine On
# Redirect any direct requests to "/viewticket.php?id=<id>" to "/viewticket/<id>"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^id=(\d*)
RewriteRule ^(viewticket)\.php$ /$1/%1 [R=301,L]
# Rewrite "/viewticket/<id>" to "viewticket.php?id=<id>"
RewriteRule ^(viewticket)/(\d*)$ $1.php?id=$2 [QSA,L]
如果您不希望您的“漂亮” /viewticket/<id>URL 上有任何查詢字串,則洗掉QSA最后一條規則中的 (Query String Append) 標志。
$1是對模式中第一個捕獲組(帶括號的子RewriteRule 模式)的反向參考。同樣,%1是對最后匹配的CondPattern(RewriteCond指令)中第一個捕獲組的反向參考。
始終首先使用 302(臨時)重定向進行測驗以避免潛在的快取問題,并且只有在您確定一切都按預期作業時才更改為 301(永久)重定向。測驗前清除瀏覽器快取。
參考:
- https://httpd.apache.org/docs/2.4/rewrite/intro.html
- https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule
額外的:
RewriteEngine On RewriteRule ^(. [^/])/$ http://%{HTTP_HOST}/$1 [R=301,L] RewriteCond %{THE_REQUEST} \s/([^.] )\.php [NC] RewriteRule ^ /%1 [NE,L,R] RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)/?$ /$1.php [L]
您現有的指令(洗掉尾部斜杠和.php擴展名)并不完全正確。
您無條件洗掉尾部斜杠的第一條規則也將“嘗試”從目錄中洗掉尾部斜杠 - 這將與 mod_dir 沖突并導致重定向回圈。正則運算式中間的否定字符類[^/](匹配非斜線字符)似乎是多余的。這也重定向到 HTTP(不是 HTTPS)?
如果它出現在 URL 的查詢字串部分中,則洗掉擴展名的第二條規則也.php將錯誤地匹配。.php例如。/foo?bar.php將被重定向到/foo?bar. 盡管正則運算式([^.] )確實避免了可能更常見的/foo.php?bar.php被錯誤重定向的情況。
The third rule that appends the .php extension via an internal rewrite could potentially result in a rewrite-loop (500 Internal Server Error) if you received a request of the form /foo/bar and /foo.php exists in the root of the filesystem. Also, the (optional) trailing slash is also captured by the capturing subgroup, since the * quantifier is greedy. This would also result in a malformed rewrite, if it wasn't for the first rule that removes the trailing slash. (But since the trailing slash is removed then the optional trailing slash in this rule is redundant.)
If you still need these rules to remove the trailing slash and handle extensionless .php URLs then have these rules as follows. Make sure you are already linking to the extensionless URLs internally. These rules should follow the rule block I posted above.
# Remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(. )/$ http://%{HTTP_HOST}/$1 [R=301,L]
# Remove ".php" extension if present on the initial request
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (. )\.php$ /$1 [R=301,L]
# Append ".php" extension if request maps to a PHP file
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
These directives (and your original directives for that matter) do assume the .htaccess file is located in the document root directory.
uj5u.com熱心網友回復:
You can use the following code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/] )/$ $1.php
RewriteRule ^([^/] )/([^/] )/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L
And if it won't works you can read this PDF file: How to remove PHP or HTML extensions with htaccess
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/455500.html
下一篇:事件監聽器
