GKWelding Posted February 17, 2010 Share Posted February 17, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/192449-regex-for-url-segment-identification/ Share on other sites More sharing options...
GKWelding Posted February 18, 2010 Author Share Posted February 18, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/192449-regex-for-url-segment-identification/#findComment-1014224 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.