[英]How? (.htaccess redirect 301)
我在localhost / gallery /文件夾中有一個站點。 我想這樣做:-當我編寫localhost / gallery / 12時,真正的地址已打開,成為localhost / gallery / index.php?url = 12,我試圖做到這一點,但不幸的是,它不起作用。 返回錯誤310(ERR_TOO_MANY_REDIRECTS)。
AddDefaultCharset UTF-8
Options +FollowSymlinks -Indexes
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.(php|html)\ HTTP/
RewriteRule ^index\.(php|html)$ / [R=301,L]
RewriteRule ^(.*)$ http://localhost/gallery/$1 [R=301,L]
RewriteBase /
RewriteRule ^([^.]+)$ index.php?url=$1 [L,NC,QSA]
以下行實際上強制執行EXTERNAL重定向:
RewriteRule ^(.*)$ http://localhost/gallery/$1 [R=301,L]
因此,對http://localhost/foobar
的請求將導致301響應,告訴客戶端請求http://localhost/gallery/foobar
。 客戶端現在向http://localhost/gallery/foobar
發出新請求 ,這將導致對http://localhost/gallery/gallery/foobar
的301響應,依此類推。
嘗試以下方法:
AddDefaultCharset UTF-8
Options +FollowSymlinks -Indexes
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.(php|html)\ HTTP/
RewriteRule ^index\.(php|html)$ / [R=301,L]
RewriteCond %{REQUEST_URI} !^/gallery/
RewriteRule ^(.*)$ http://localhost/gallery/$1 [R=301,L]
RewriteBase /
RewriteRule ^gallery/([^.]+)/?$ gallery/index.php?url=$1 [NC,QSA,L]
請注意,我在無限循環規則之前添加了一個重寫條件,以阻止已經以/ gallery開頭的URL激活該重定向。
另外,我還修改了文件規則,以正確地將http://localhost/gallery/foobar
重寫為您描述的http://localhost/gallery/index.php?url=foobar
。 注意/?
該模式為您提供了一個可選的結尾斜杠,以便http://localhost/gallery/12
和http://localhost/gallery/12/
都可以使用。 如果不想使用后者,請刪除/?
從模式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.