Adding more directories to url using mod_rewrite

Deadly 提交于 2020-01-05 04:51:34

问题


I am using mod_write to pull urls out of my database. I have it working at a basic level, but what I am stuck on is creating complex URLs using various slugs pulled from the database as directories in the URL.

Currently the url before using mod_rewrite looks like:

www.domainname.co.uk/company?=1&staff=3

Which takes you to a staff profile within an appropriately styled company page.

But what I want the URL to look like is:

www.domainname.co.uk/COMPANY-NAME/STAFF-NAME

My htaccess currently looks like:

RewriteEngine On
RewriteRule ^c/(.*)$ ./company.php?company=$1

Which results in the URL

www.domainname.co.uk/c/COMPANY-NAME

How do I add the second level directory?


回答1:


You can use on additional rule for that:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)/?$ company.php?company=$1&staff=$2 [L,QSA]

RewriteRule ^c/([\w-]+)/?$ company.php?company=$1 [L,QSA,NC]



回答2:


RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-\.]+)/([a-zA-Z0-9_-\.]+)(/?)$ /company.php?company=$1&staff=$2 [NC,L]


That should allow urls of the type:
www.domainname.co.uk/COMPANY-NAME/STAFF-NAME


来源:https://stackoverflow.com/questions/39100195/adding-more-directories-to-url-using-mod-rewrite

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