[英]301 redirect query string to SEO friendly URLs through .htaccess
我在.htaccess文件上寫了一些代碼,它允許使用SEO友好的URL而不是丑陋的查詢字符串。 以下代碼將瀏覽器中的SEO友好版本重寫為服務器上的查詢字符串版本。
RewriteEngine On
RewriteRule ^seo/([^/]*)/$ /directory/script.php?size=large&colour=green&pattern=$1 [L]
這樣丑陋
http://www.mysite.com/directory/script.php?size=large&colour=green&pattern=striped
現在很美
http://www.mysite.com/directory/seo/striped/
只是為了解釋一下代碼; seo
用於向URL添加更多關鍵字, /directory/
是.htaccess文件所在的目錄,參數size=large
, colour=green
永遠不會更改,而pattern=$1
可以是許多不同的值。
上面的代碼完美無缺。 但是,問題是我現在卡在兩個指向完全相同內容的URL中。 為了解決這個問題,我想將舊的,丑陋的查詢字符串重定向到SEO友好URL。 到目前為止我所嘗試的並不起作用 - 谷歌今天並不是特別友好。
任何人都可以提供工作代碼來放入我的.htaccess文件,將丑陋的重定向到新的URL,同時保留重寫? 謝謝!
這應該做的伎倆:
RewriteEngine On
## Redirect to pretty urls
# The '%1' in the rewrite comes from the group in the previous RewriteCond
RewriteCond %{REQUEST_URI} !seo
RewriteCond %{QUERY_STRING} ^size=large&colour=green&pattern=([a-zA-Z]*)$
RewriteRule (.*) /directory\/seo\/%1\/? [L,R=301]
## Rewrite to long url, additional parameter at the end will cause
## the internal redirect not to match the previous rule (would cause redirect loop)
RewriteRule ^directory\/seo\/([^/]*)/$ /directory/script.php? size=large&colour=green&pattern=$1&rewrite [L]
如果需要,您還可以匹配大小和顏色,也可以將它們更改為正則表達式組,並使用相應的%N
希望這可以幫助。
未經測試,但這可能有效......
RewriteRule ^目錄/ script.php?size = large&color = green&pattern =(。*)$ / seo / $ 1 /? [R = 301,NE,NC,L]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.