I have created a htaccess rewrite code f开发者_开发百科or URLs so when a user goes to myurl.com/testing/
it shows them index.php?page=testing
however I would like to have a second or maybe third page so it could look like myurl.com/testing/2832/9283
and would show users index.php?page=testing&var1=2832&var2=9283
.
This is the code I currently have:
RewriteRule ^([^\/]+)/([^\/]*)/$ index.php?page=$1&var1=$2
RewriteRule ^([^\/]+)/([^\/]*)$ index.php?page=$1&var1=$2
This works but I want to make the variables optional. If I do not have a second variable (i.e. just myurl.com/testing/
) then it says it cant find the file.
# 3-level deep parameters
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)(/([^/]+))?(/([^/]+))?(/)?$ /index.php?page=$1&var1=$3&var2=$5 [QSA,L]
This rule will not touch already existing files and folders.
This rule will rewrite:
/help/tracking/123456/
=>/index.php?page=help&var1=tracking&var2=123456
/help/tracking
=>/index.php?page=help&var1=tracking&var2=
/help
=>/index.php?page=help&var1=&var2=
You were having page=index.php
because your rule rewrites already rewritten URLs (A lot of people forgetting, that when rewrite happens, it goes to next iteration and starting to test all rules again). This rule has conditions (extra checks) to ignore already existing files and folders.
Why not just set multiple RewriteRules for each case?
RewriteRule ^([^/]+)/?$ index.php?page=$1
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?page=$1&var1=$2
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?page=$1&var1=$2&var2=$3
精彩评论