Jump to content

regex for URL segment identification


GKWelding

Recommended Posts

I have the following code in my htaccess file...

 

RewriteCond %{REQUEST_URI} ^/clinics/dashboard/.*
RewriteRule ^(.*)$ index.php?$1 [L]

 

This works fine when I got to http://www.{myurl}.com/clinics/dashboard/ and redirects me to the correct controller. However, if I do http://www.{myurl}.com/clinics/dashboard then I get a 404 error. Regex isn't my strong point at all, so does anybody know how I could add a regular expression to look to see if the character after dashboard is either a / or whether it's the end of the string. I know that to find out if the next character isn't a / or a . is...

 

RewriteCond %{REQUEST_URI} ^/clinics/dashboard[^/\.].*
RewriteRule ^(.*)$ index.php?$1 [L]

 

and that to match a / at the end of the url string is...

 

RewriteCond %{REQUEST_URI} ^/clinics/dashboard[/$].*
RewriteRule ^(.*)$ index.php?$1 [L]

 

However, my limited regex knowledge has failed me and I can't seem to modify this to do match either a / at the end of the string or nothing at all, and trust me, I've googled my little heart out...

 

Thanks in advance for your replies, they will be much appreciated.

Link to comment
Share on other sites

managed to solve it myself by doing the following...

 

RewriteCond %{REQUEST_URI} ^/clinics/(dashboard)([a-zA-Z0-9_-\.]+)$
RewriteRule ^(.*)$ index.php?/frontend/index/$1$2 [L]

RewriteCond %{REQUEST_URI} ^/clinics/dashboard.*
RewriteRule ^(.*)$ index.php?$1[L]

 

The first rewrite condition determines whether alphanumeric character,  _, - or a . is the first character after the url. If it is then redirect to the first function. If it isn't then assume the url is either url.com/dashboard or url.com/dashboard/ rather than url.com/dashboard.html or url.com/dashboard-view.html.

Link to comment
Share on other sites

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.