重写URL的htaccess对我的网站不起作用

时间:2014-07-12 03:59:28

标签: php regex apache .htaccess mod-rewrite

我有动态网址(irasol.com),而我导航到菜单,网址显示为

http://irasol.com/index.php?id=1

我想要这样的网址

domainname/home
domainname/aboutus
domainname/contactus
domainname/apply

home,aboutus,contactus,apply是已经在数据库中的菜单名称。

我的htaccess文件是

Options +FollowSymLinks
RewriteEngine on

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

2 个答案:

答案 0 :(得分:2)

请改用:

Options -Multiviews
RewriteEngine on

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]

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

<强>解释

  • 前三个条件确保domainname/aboutus不是真实文件,因此我们不会重写已存在的文件。
  • Options -Multiviews消除了许多潜在问题

答案 1 :(得分:0)

在您当前的代码中,摆脱模式中的.php:

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

您在请求中没有匹配.php扩展名。您只是将匹配路由到真实的.php扩展名

上的查询字符串

至于更好的解决方案:

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