簡體   English   中英

.htaccess 使用重寫規則重定向 301

[英].htaccess redirect 301 using rewrite rule

我正在嘗試美化一些網址。 我配置了 htaccess 文件,所以我的網址被更改:

old url: http://mysite.com/index.php?id=45tye4 new url: http://mysite.com/45tye4

我現在想將舊網址永久重定向到新網址。 這是我沒有運氣的嘗試:

RewriteRule ^index.php?id=(.*)$ $1 [R=301,L]

主要問題似乎是“?” 在 url 中。 當我嘗試相同的 url 時沒有? 重定向有效。 我還嘗試了其他沒有運氣的變體:

RewriteRule ^index.php\?id=(.*)$ $1 [R=301,L]
RewriteRule ^index.php[\?]id=(.*)$ $1 [R=301,L]

更新:

我根據 anubhava 指令添加了重定向。 重定向有效,但不幸的是我進入了重定向循環。 我認為 [L] 標志應該解決重定向循環,但事實並非如此。

RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^index\.php/?$ /%1? [R=301,L] 

RewriteRule ^(.*)$ index.php?id=$1 [L]

RewriteRule 僅匹配 REQUEST_URI。 您必須使用 RewriteCond 來匹配查詢字符串

試試這個代碼:

Options +FollowSymlinks -MultiViews
RewriteEngine on

RewriteCond %{THE_REQUEST} ^GET\s/+index\.php [NC]
RewriteCond %{QUERY_STRING} (^|&|\?)id=(.*)(&|$) [NC]
RewriteRule . /%2? [R=301,L,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?id=$1 [L]

這會將/index.php?id=45tye4的舊 URI 重定向到新 URI: /45tye4

暫無
暫無

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

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