在PHP中使用htaccess重写URL
我正在尝试使用htaccess重写规则获取SEO友好的URL。请在下面找到它们在PHP中使用htaccess重写URL,php,apache,.htaccess,mod-rewrite,url-rewriting,Php,Apache,.htaccess,Mod Rewrite,Url Rewriting,我正在尝试使用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_n
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配置作为子域时
我的原始URL如下所示
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
规则不起作用,并给出如下的内部服务器错误
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+印度
在页面中调用URL格式的是href='/facebook/7up+India'
最终的预期url是这样的http://example.com/facebook/7up+印度请尝试以下代码:
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]
你的页面URL是什么样子的?我的页面URL看起来像base\u URL/brands.php,base\u URL/facebook.php?fb\u page\u id=3343131343,base\u URL/twitter.php?twitter\u screen\u name=dhjhsjsjadhar你确定Apache的其他配置正确吗?尝试删除.htaccess和check中的所有代码?尝试注释选项+FollowSymLinks,这可能是个问题。其他规则似乎很好。让我们来看看。您是否查看了服务器错误日志,如错误所示?它可能包含有价值的信息。请检查问题中或下面的原始url格式。base_url/facebook.php/fb_page_name=7up+India base_url/twitter.php/twitter_screen_name=7up+IndiaI在apache未定义索引的错误日志中出现错误:fb_page_id这是我在各个页面中收集GET参数的方式,preg_替换“[^0-9a-zA-Z]i”,urldecode$GET['fb_page_name';preg_替换“[^0-9a-zA-Z]i”,urldecode$获取['twitter_屏幕名称];我在问题结束时更新了所有的url格式是的,我更新了你的规则,但尝试不工作