Jump to content

Quick .htaccess question - not have trailing slash after file re-write


Recommended Posts

Hi all,

 

Trying to figure out why the page I'm creating works when I place a trailing slash but 404's when i take it off...trying to get it to not have it.

 

For example, this code:

 

RewriteRule ^([A-Za-z0-9\-+\(\)#%|_,]+-test-page)([A-Za-z0-9\-/+_\(\)]+)$ file.php?city=$1&query=$2 [L]

 

results in: example DOT com/city-test-page/ and if I take off the trailing slash it goes to a 404 but would like it to be the other way around.

 

Also when I take the "([A-Za-z0-9\-/+_\(\)]+)" and "&query=$2" out it works correctly in regards to trailing slash but they query doesn't work.

 

Any thoughts?

What are you expecting the &query part to contain? The simple answer is that with the trailing slash of your URI the second capture group has something to capture (the /), without it it doesn't match anything, meaning your pattern doesn't match so the RewriteRule won't get applied. Something like this would probably be more approriate, as the second capture group doesn't have a required width and will not contain the trailing slash. Alternatively you could write one RewriteRule for 1 'directory' deep and another for 2.

 

RewriteRule ^([A-Za-z0-9-]+-test-page)/?([A-Za-z0-9-]*)$ file.php?city=$1&query=$2 [L]

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.