Forum Moderators: phranque
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule (.*)/(.*)/(.*)/$ /main.php?state=$1&city=$2&listing=$3
RewriteRule (.*)/(.*)/$ /main.php?state=$1&city=$2
RewriteRule (.*)/$ /main.php?state=$1
So basically I can have the following urls
domain.com/texas/
domain.com/texas/dallas/
domain.com/texas/dallas/listing/
But I need to be able to also show brands at a state and/or city level along with the others. Something like this
domain.com/texas/brand1/
domain.com/texas/brand2/
domain.com/texas/dallas/brand1/
domain.com/texas/dallas/brand2/
Is this even possible without messing up the original rewrites? Would I need to add a new delimiter to show a difference between the two? I am at the design stage so I can design it however.
1st I would change this:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule (.*)/(.*)/(.*)/$ /main.php?state=$1&city=$2&listing=$3
RewriteRule (.*)/(.*)/$ /main.php?state=$1&city=$2
RewriteRule (.*)/$ /main.php?state=$1
to:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ([^/]+)/([^/]+)/([^/]+)/$ /main.php?state=$1&city=$2&listing=$3 [L]
RewriteRule ([^/]+)/([^/]+)/$ /main.php?state=$1&city=$2 [L]
RewriteRule ([^/]+)/$ /main.php?state=$1 [L]
Always use the [L] last flag unless you know you do not need it.
[^/]+ = 1 or more of anything that is not a /, slightly more efficient, because there is no need to 'back-track' for any reason EG break the match at the /
2nd I would change the /main directory to /1main for efficiency - you should see why in a minute:
RewriteRule ([^/]+)/([^/]+)/([^/]+)/$ /1main.php?state=$1&city=$2&listing=$3
RewriteRule ([^/]+)/([^/]+)/$ /1main.php?state=$1&city=$2
RewriteRule ([^/]+)/$ /1main.php?state=$1
3rd I would add my skips:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
# if the URL has already been rewritten, skip the rewrite rules, we know there is no additional rewrite necessary if the URL starts with a 1.
RewriteRule ^1 - [S=7]
# if the URL does not contain the word product, skip the next 3 rules to the other rewrite section. This allows us to only need to match the a word once, and if we do, let the 'catch-alls' act on it:
RewriteRule!product - [S=3]
RewriteRule ([^/]+)/([^/]+)/([^/]+)/$ /1product.php?state=$1&city=$2&listing=$3 [L]
RewriteRule ([^/]+)/([^/]+)/$ /1product.php?state=$1&city=$2 [L]
RewriteRule ([^/]+)/$ /1product.php?state=$1 [L]
RewriteRule ([^/]+)/([^/]+)/([^/]+)/$ /1main.php?state=$1&city=$2&listing=$3 [L]
RewriteRule ([^/]+)/([^/]+)/$ /1main.php?state=$1&city=$2 [L]
RewriteRule ([^/]+)/$ /1main.php?state=$1 [L]
The ordering of these will have an impact on efficiency, so you will want to stucture the most used URL's to match the 1st set, this could be done by reversing the order of the catch-all rules and changing the skip line to:
RewriteRule product - [S=3]
Hope this helps.
Justin
Edited: missed a - in the skip rule
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^1 - [S=10]
RewriteRule all - [S=6]
RewriteRule ([^/]+)/([^/]+)/([^/]+)/$ /1main.php?state=$1&city=$2&listing=$3 [L]
RewriteRule ([^/]+)/([^/]+)/([^/]+)$ /1main.php?state=$1&city=$2&listing=$3 [L]
RewriteRule ([^/]+)/([^/]+)/$ /1main.php?state=$1&city=$2 [L]
RewriteRule ([^/]+)/([^/]+)$ /1main.php?state=$1&city=$2 [L]
RewriteRule ([^/]+)/$ /1main.php?state=$1 [L]
RewriteRule ([^/]+)$ /1main.php?state=$1 [L]
RewriteRule ([^/]+)/([^/]+)/([^/]+)-all/$ /1product.php?state=$1&city=$2&make=$3 [L]
RewriteRule ([^/]+)/([^/]+)/([^/]+)-all$ /1product.php?state=$1&city=$2&make=$3 [L]
RewriteRule ([^/]+)/([^/]+)-all/$ /1product.php?state=$1&make=$2 [L]
RewriteRule ([^/]+)/([^/]+)-all$ /1product.php?state=$1&make=$2 [L]
RewriteRule ([^/]+)/([^/]+)/([^/]+)/$ /1main.php?state=$1&city=$2&listing=$3 [L]
RewriteRule ([^/]+)/([^/]+)/([^/]+)$ /1main.php?state=$1&city=$2&listing=$3 [L]
RewriteRule ([^/]+)/([^/]+)/([^/]+)[b]/?$[/b] /1main.php?state=$1&city=$2&listing=$3 [L]
Jim
#start
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^1 - [S=10]
RewriteRule all - [S=6]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ $1/$2/$3/ [R]
RewriteRule ([^/]+)/([^/]+)/([^/]+)/$ /1main.php?state=$1&city=$2&listing=$3 [L]
RewriteRule ^([^/]+)/([^/]+)$ $1/$2/ [R]
RewriteRule ([^/]+)/([^/]+)/$ /1main.php?state=$1&city=$2 [L]
RewriteRule /^([^/]+)$ $1/ [R]
RewriteRule ([^/]+)/$ /1main.php?state=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ $1/$2/$3/ [R]
RewriteRule ([^/]+)/([^/]+)/([^/]+)-all/$ /1product.php?state=$1&city=$2&make=$3 [L]
RewriteRule ^([^/]+)/([^/]+)$ $1/$2/ [R]
RewriteRule ([^/]+)/([^/]+)-all/$ /1product.php?state=$1&make=$2 [L]
#end
Everything seems to be working but the one line
RewriteRule /^([^/]+)$ $1/ [R]
domain.com/first-var is the only one that doesn't add the / at the end. domain.com/first-var/second-var and also with the third var redirects fine.
domain.com/first-var just shows a 404 error. I tried removing the first "/" in the "RewriteRule /^([^/]+)$ $1/ [R]" but that causes issues with the whole domain. Any ideas how to get the first var working?
RewriteRule /^([^/]+)$ $1/ [R]
RewriteRule ^/([^/]+)$ http://www.example.com/$1/ [R=301]
Jim