簡體   English   中英

htaccess mod_rewrite規則組合問題

[英]htaccess mod_rewrite rules combination issue

我是apache的新手,所以這是一個基本問題,但是我無法使用其他問題或鏈接來解決。
我試圖使用.htaccess更改URL ,但我有兩個目的:

  1. 隱藏.php擴展名
  2. 將一些queryString從somefile.php?id=bar更改為somefile/id/bar ,其中bar是數字或混合字符串,例如T51-3
    我的queryString是http://localhost/payment/theme/ticket?id=770314 ,我想將其更改為http://localhost/payment/theme/ticket/id/770314
    http://localhost/payment/theme/ticket/770314

我發現此代碼:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]


## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]


stackoverflow.com/this_question中

這個工作很好,但是沒有解決我的第二個問題。
因此,我在該網站和其他網站中搜索了一段時間,並確實找到了一些像這樣的示例代碼:

RewriteRule ^([a-zA-Z0-9_-]+)-([0-9]+)$ ticket.php?id=$2

要么

RewriteRule ^(.*)$ /ticket.php?url=$1 [L]

等等...

但是這些都不適合我。
其中一些可以使服務器完成工作,而另一些則完全沒有影響。 您能指導我正確的方法嗎?
我看着這些問題:
htaccess重寫查詢字符串

如何使用.htaccess更改名稱的查詢字符串參數?

如何使用.htaccess隱藏.php URL擴展?

這些鏈接:
http://simonecarletti.com/blog/2009/01/apache-query-string-redirects/
https://wiki.apache.org/httpd/RewriteQueryString

更新:
這是我的網站結構: 網站結構
htaccess用於重要的文件夾,例如formsdbclasses等。
如圖所示,我所有的網站頁面都位於theme文件夾中。
在htaccess根目錄中,只有兩行自動啟動會話。

我將您的代碼添加到主題的htaccess中。
Thanks in Advance

您可以在

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /payment/theme/

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php\?(\w+)=([^\s&]+)\s [NC]
RewriteRule ^ %1/%2/%3? [R,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# to handle http://localhost/payment/theme/ticket/id/770314
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)/([\w-]+)/([\w-]+)/?$ $1.php?$2=$3 [L,QSA]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

暫無
暫無

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

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