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
https://forums.phpfreaks.com/topic/192449-regex-for-url-segment-identification/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.