避免从URL引用index.php?page =的htaccess

问题描述:

我的项目有类似 http://domain.com/index.php?page之类的URL。 = home / contactus http://domain.com/index。 php?page = events / announcementdetails / 80 等。

我想避免使用 index.php?page = 和给定URL中的参数(例如80),并希望将URL更改为 http://domain.com/home/contactus http://domain.com/events/announcementdetails 。我该如何使用htaccess文件来实现这一点。

I want to avoid the index.php?page= and the parameters like 80 in the given URL and want to change the URL to http://domain.com/home/contactus and http://domain.com/events/announcementdetails. How can I achieve this using htaccess file.

启用 mod_rewrite .htaccess 通过 httpd.conf ,然后将此代码放入 DOCUMENT_ROOT / .htaccess 文件:

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

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

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