.htaccess and changing php queries to a clean URL

2019-08-29 04:15发布

问题:

I am trying to change the URL structure for my clients website, i have never worked with a htaccess file before so forgive me if its a stupid question. i have spent time looking on stack overflow, google and youtube but for the life of me i just cant get my head around this.

The URL Current structure: jobtitle being the variable and Administration-Jobs being the value

http://localhost/website/job_title.php?jobtitle=Administration-Jobs    

the URL structure the client has requested

http://localhost/website/Administration-Jobs

The htaccess file i have attempted:

RewriteEngine On
RewriteRule ^jobs_by_title.php/([0-9a-zA-Z_-]) job_title.php?jobtitle=$1    

also once i have this ht access file working, what should be required in the php script to go to the clean version rather then the dirty one? or is this done automatically in the htaccess?

EDIT:

I have got it working now with

^([A-Za-z0-9_-]+)?$ job_title.php?jobtitle=$1 [L]    

回答1:

Your regex for capturing URI is wrong.

Place this code in /website/.htaccess:

RewriteEngine On
RewriteBase /website/

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


回答2:

Try this:

RewriteEngine On
RewriteRule ^(\w-]+)/?$ job_title.php?jobtitle=$1


回答3:

I have finally got it working with

RewriteRule ^([A-Za-z0-9_-]+)?$ job_title.php?jobtitle=$1 [L]