[英]Remove File Extension and URL Variable using .htaccess
我正在自己整理一個小博客,發現一個有用的.htaccess文件來刪除文件擴展名:
AddType text/x-component .htc
RewriteEngine On
RewriteBase /
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php
這工作得很好,所有頁面顯示的.php更少。 我知道想要擴展它,因此當我單擊指向特定博客文章的鏈接(例如/blog/index.php?art=1)時,它只會在url中顯示為website / blog / 1。 我想標記到.htaccess文件的末尾:
RewriteRule ^blog/(.*)$ blog/index.php?art=$0 [L]
但這似乎不起作用。 編輯實際上,它會中斷博客頁面,因此不會從數據庫中提取任何摘錄
我的.htaccess文件位於根目錄中,博客文件位於/root/blog/index.php中
任何幫助將不勝感激
與大多數其他語言不同, .htaccess
中的參數不是基於0的。 要訪問第一個參數,應使用$1
,而不是$0
。
以下應該工作:
RewriteRule ^blog/(.*)$ blog/index.php?art=$1 [L]
也可能值得在其中添加一些測試,例如,您可能只希望將數值傳遞給art
,因此可以使用以下方法進行改進:
RewriteRule ^blog/([0-9]+)$ blog/index.php?art=$1 [L]
另外,可能值得添加QSA
標志,因為這還將保留原始URL中傳遞的所有查詢字符串:
RewriteRule ^blog/([0-9]+)$ blog/index.php?art=$1 [L,QSA]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.