简体   繁体   English

在PHP中使用htaccess重写URL

[英]URL rewrite using htaccess in PHP

I am trying to get SEO friendly urls using htaccess rewrite rules. 我正在尝试使用htaccess重写规则获取SEO友好的URL。 Please find them below. 请在下面找到它们。

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]

I verified in my localhost all the link they are working fine in xampp environment. 我在本地主机中验证了所有链接在xampp环境中均能正常工作。 But when i hosted them in remote server with OS centos and virtualhost configuration for a subdomain. 但是,当我将它们托管在具有OS centos和用于子域的virtualhost配置的远程服务器中时。

My Original urls will be like below. 我的原始网址如下所示。

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

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

The rules are not working and giving internal server error like below. 规则不起作用,并出现如下所示的内部服务器错误。

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

So i verified in remote server with simple htaccess redirection rules like below one to main site. 因此,我在远程服务器上通过简单的htaccess重定向规则进行了验证,例如以下一个到主站点的重定向规则。 It worked perfectly. 效果很好。 I am not able to find what went wrong. 我无法找到问题所在。

Redirect 301 / http://example.com/

My Original URL format was this http://example.com/facebook.php/fb_page_name=7up+India Calling URL format in pages was this href='/facebook/7up+India' Final expecting url was this http://example.com/facebook/7up+India 我的原始URL格式是http://example.com/facebook.php/fb_page_name=7up+India页面中的调用URL格式是this href='/facebook/7up+India'最后的期望URL是此http://example.com/facebook/7up+India

Try below code: 试试下面的代码:

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]

You missed to add $ , which indicates the end of regular expression. 您错过了添加$ ,它指示正则表达式的结尾。

Have your rules like this: 有这样的规则:

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