.htaccess rewriteRule (./index.php?page=projects&id=$1)

不问归期 提交于 2019-12-20 05:26:16

问题


I'm trying to rewrite the URL using htaccess, but I have some problems that I hope you could help me with.

This is the contents of my .htaccess file

RewriteEngine On  

RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f  

RewriteRule ^(.*)$ ./index.php?page=projects&id=$1

The following rule works great, it converts this url:

peterboruplund.dk/?page=projects&id=projectname

to this

peterboruplund.dk/projectname

But the problem is that I also want to be able to only view a page. Like if I write this url:

peterboruplund.dk/?page=info

It should go to the page called "info", like this:

peterboruplund.dk/info

My problem is how can I achieve both things at the same time?

best, Peter


回答1:


You can have another rule for /info:

RewriteEngine On  

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

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


来源:https://stackoverflow.com/questions/25238393/htaccess-rewriterule-index-phppage-projectsid-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!