簡體   English   中英

在PHP中使用htaccess重寫URL

[英]URL rewrite using htaccess in PHP

我正在嘗試使用htaccess重寫規則獲取SEO友好的URL。 請在下面找到它們。

Options +FollowSymLinks
RewriteEngine on

RewriteRule ^brands brands.php [NC,L]

RewriteRule ^facebook/([0-9]+) facebook.php?fb_page_id=$1 [NC,L]
RewriteRule ^twitter/([0-9a-zA-Z]+) twitter.php?twitter_screen_name=$1 [NC,L]

我在本地主機中驗證了所有鏈接在xampp環境中均能正常工作。 但是,當我將它們托管在具有OS centos和用於子域的virtualhost配置的遠程服務器中時。

我的原始網址如下所示。

base_url/facebook.php/fb_page_name=7up+India

base_url/twitter.php/twitter_screen_name=7up+India

規則不起作用,並出現如下所示的內部服務器錯誤。

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, root@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.
enter code here

因此,我在遠程服務器上通過簡單的htaccess重定向規則進行了驗證,例如以下一個到主站點的重定向規則。 效果很好。 我無法找到問題所在。

Redirect 301 / http://example.com/

我的原始URL格式是http://example.com/facebook.php/fb_page_name=7up+India頁面中的調用URL格式是this href='/facebook/7up+India'最后的期望URL是此http://example.com/facebook/7up+India

試試下面的代碼:

Options +FollowSymLinks
RewriteEngine on

RewriteRule ^brands$ brands.php [NC,L]

RewriteRule ^facebook/([0-9]+)$ facebook.php?fb_page_id=$1 [NC,L]
RewriteRule ^twitter/([0-9a-zA-Z]+)$ twitter.php?twitter_screen_name=$1 [NC,L]

您錯過了添加$ ,它指示正則表達式的結尾。

有這樣的規則:

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /

RewriteRule ^brands brands.php [NC,L]

RewriteCond %{THE_REQUEST} \s/+facebook\.php\?fb_page_id=([^\s&]+) [NC]
RewriteRule ^ facebook/%1? [R=302,L,NE]

RewriteCond %{THE_REQUEST} \s/+twitter\.php\?twitter_screen_name=([^\s&]+) [NC]
RewriteRule ^ twitter/%1? [R=302,L,NE]

RewriteRule ^facebook/([^/]+)/?$ facebook.php?fb_page_id=$1 [NC,L,QSA]
RewriteRule ^twitter/([^/]+)/?$ twitter.php?twitter_screen_name=$1 [NC,L,QSA]

暫無
暫無

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

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