immanuelx2 Posted July 21, 2009 Share Posted July 21, 2009 Currently, I have: RewriteEngine on # Turn http://mydomain.com into http://www.mydomain.com/ RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC] RewriteRule ^(.*)$ http://www.mydomain.com/$1 [N,R=301] RewriteRule ^(.*?)/(.*?)/([0-9]+)-(.*?)$ index.php?type=$1&category=$2&articleid=$3&name=$4 An example page would be: http://www.mydomain.com/news/category/12345-this-is-the-article/ So if someone pointed to http://www.mydomain.com/news it should take them to index.php?type=news Or if someone pointed to http://www.mydomain.com/news/hardware it should take them to index.php?type=news&category=hardware How should I set up my other Rewrite rules? I'm thinking of some sort of hierarchy/tree structure like: RewriteRule ^(.*?)$ index.php?type=$1 RewriteRule ^(.*?)/(.*?) index.php?type=$1&category=$2 RewriteRule ^(.*?)/(.*?)/([0-9]+)-(.*?)$ index.php?type=$1&category=$2&articleid=$3 ..But I'm not sure if that would work or if I'm even on the right track. Any help is greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/166856-mod-rewrite-how-should-i-set-it-up/ Share on other sites More sharing options...
dreamwest Posted July 21, 2009 Share Posted July 21, 2009 Set your default type RewriteRule ^(.*)$ index.php?type=default_name Depending on your interface you need a anchor RewriteRule ^News/(.*?) index.php?type=News&category=$1 Otherwise it can be dynamic based on the hyperlink, but this can cause issues with your article id rule RewriteRule ^(.*?)/(.*?) index.php?type=$1&category=$2 This needs an anchor if you use the above dynamic rule RewriteRule ^News/(.*?)/([0-9]+)-(.*?)$ index.php?type=News&category=$1&articleid=$2 Quote Link to comment https://forums.phpfreaks.com/topic/166856-mod-rewrite-how-should-i-set-it-up/#findComment-879870 Share on other sites More sharing options...
immanuelx2 Posted July 22, 2009 Author Share Posted July 22, 2009 Set your default type RewriteRule ^(.*)$ index.php?type=default_name Depending on your interface you need a anchor RewriteRule ^News/(.*?) index.php?type=News&category=$1 Otherwise it can be dynamic based on the hyperlink, but this can cause issues with your article id rule RewriteRule ^(.*?)/(.*?) index.php?type=$1&category=$2 This needs an anchor if you use the above dynamic rule RewriteRule ^News/(.*?)/([0-9]+)-(.*?)$ index.php?type=News&category=$1&articleid=$2 Thanks for the reply. I'm a bit confused on what you mean by interface and anchor.. Could you briefly explain those for me please? Quote Link to comment https://forums.phpfreaks.com/topic/166856-mod-rewrite-how-should-i-set-it-up/#findComment-880010 Share on other sites More sharing options...
dreamwest Posted July 24, 2009 Share Posted July 24, 2009 You have 3 dynamic rules with no write stops [L,QSA] so its gonna keep writing into your next rule You can still have it like you had but use anchors RewriteRule ^(.*?)$ index.php?type=$1 RewriteRule ^Anchor_name_(.*?)/(.*?) index.php?type=$1&category=$2 RewriteRule ^Another_anchor_/(.*?)/([0-9]+)-(.*?)$ index.php?type=$1&category=$2&articleid=$3 If you dont have write rules you cant have 3 dynamic roots RewriteRule ^ DYNAMIC ROOT --> (.*?)$ index.php?type=$1 RewriteRule ^ DYNAMIC ROOT --> (.*?)/(.*?) index.php?type=$1&category=$2 RewriteRule ^ DYNAMIC ROOT --> (.*?)/(.*?)/([0-9]+)-(.*?)$ index.php?type=$1&category=$2&articleid=$3 Quote Link to comment https://forums.phpfreaks.com/topic/166856-mod-rewrite-how-should-i-set-it-up/#findComment-881635 Share on other sites More sharing options...
immanuelx2 Posted July 24, 2009 Author Share Posted July 24, 2009 You have 3 dynamic rules with no write stops [L,QSA] so its gonna keep writing into your next rule You can still have it like you had but use anchors RewriteRule ^(.*?)$ index.php?type=$1 RewriteRule ^Anchor_name_(.*?)/(.*?) index.php?type=$1&category=$2 RewriteRule ^Another_anchor_/(.*?)/([0-9]+)-(.*?)$ index.php?type=$1&category=$2&articleid=$3 If you dont have write rules you cant have 3 dynamic roots RewriteRule ^ DYNAMIC ROOT --> (.*?)$ index.php?type=$1 RewriteRule ^ DYNAMIC ROOT --> (.*?)/(.*?) index.php?type=$1&category=$2 RewriteRule ^ DYNAMIC ROOT --> (.*?)/(.*?)/([0-9]+)-(.*?)$ index.php?type=$1&category=$2&articleid=$3 I think I'm starting to understand... So you are saying I need something like this: RewriteRule ^news/$ index.php?type=news RewriteRule ^news/(.*?) index.php?type=news&category=$2 RewriteRule ^news/(.*?)/([0-9]+)-(.*?)$ index.php?type=news&category=$2&id=$3 RewriteRule ^forum/$ index.php?type=forum RewriteRule ^forum/(.*?) index.php?type=forum&category=$2 RewriteRule ^forum/(.*?)/([0-9]+)-(.*?)$ index.php?type=forum&category=$2&id=$3 Is that correct? Quote Link to comment https://forums.phpfreaks.com/topic/166856-mod-rewrite-how-should-i-set-it-up/#findComment-882369 Share on other sites More sharing options...
dreamwest Posted July 24, 2009 Share Posted July 24, 2009 Yes. You can also simplify it resulting in a lot less work and lines #default news page -> http://www.site.com/news/ RewriteRule ^news/$ index.php?type=news #working news section -> http://www.site.com/news/cat1/ OR http://www.site.com/news/cat1/3 RewriteRule ^news/(.*?)/([0-9]+)-(.*?)$ index.php?type=news&category=$1&id=$2 Quote Link to comment https://forums.phpfreaks.com/topic/166856-mod-rewrite-how-should-i-set-it-up/#findComment-882393 Share on other sites More sharing options...
immanuelx2 Posted July 25, 2009 Author Share Posted July 25, 2009 Yes. You can also simplify it resulting in a lot less work and lines #default news page -> http://www.site.com/news/ RewriteRule ^news/$ index.php?type=news #working news section -> http://www.site.com/news/cat1/ OR http://www.site.com/news/cat1/3 RewriteRule ^news/(.*?)/([0-9]+)-(.*?)$ index.php?type=news&category=$1&id=$2 Hmm.. It appears as though only the website/news/ works... if I add category or id ( website/news/category/ ) it says page does not exist.. Also, if the url is website/news instead of website/news/ (with the ending slash), it doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/166856-mod-rewrite-how-should-i-set-it-up/#findComment-882416 Share on other sites More sharing options...
dreamwest Posted July 25, 2009 Share Posted July 25, 2009 Your right the rules are combining Try this #default news page -> http://www.site.com/news/ RewriteRule ^news/$ index.php?type=news [L,QSA] #working news section -> http://www.site.com/news/cat1/ OR http://www.site.com/news/cat1/3 RewriteRule ^news/(.*?)/([0-9]+)-(.*?)$ index.php?type=news&category=$1&id=$2 [L,QSA] If that doesnt work youll have to make the root unique #default news page -> http://www.site.com/news/ RewriteRule ^news/$ index.php?type=news [L,QSA] #working news section -> http://www.site.com/newscat/cat1/ OR http://www.site.com/newscat/cat1/3 RewriteRule ^newscat/(.*?)/([0-9]+)-(.*?)$ index.php?type=news&category=$1&id=$2 [L,QSA] Quote Link to comment https://forums.phpfreaks.com/topic/166856-mod-rewrite-how-should-i-set-it-up/#findComment-882425 Share on other sites More sharing options...
immanuelx2 Posted July 25, 2009 Author Share Posted July 25, 2009 Didn't work So how come some sites can have rewrites for /news, /news/category and even /news/category/articleid ?? Quote Link to comment https://forums.phpfreaks.com/topic/166856-mod-rewrite-how-should-i-set-it-up/#findComment-882474 Share on other sites More sharing options...
dreamwest Posted July 25, 2009 Share Posted July 25, 2009 So how come some sites can have rewrites for /news, /news/category and even /news/category/articleid ?? They know more regex than i do and theyre using write rules - which i havnt gotten into yet Anyway the example i gave you is sound, you most likely problem is RewriteRule ^news/$ index.php?type=news [L,QSA] its taking all urls with news - change to RewriteRule ^news/ index.php?type=news [L,QSA], so your url is http://www.site.com/news/. Then add your other rule in This site is a classic example of rewrite http://www.findtail.com Quote Link to comment https://forums.phpfreaks.com/topic/166856-mod-rewrite-how-should-i-set-it-up/#findComment-882497 Share on other sites More sharing options...
immanuelx2 Posted July 25, 2009 Author Share Posted July 25, 2009 What if I can get the entire string (eg. "news/category/id-title") through a $_GET[] variable and just parse it via explode() in PHP? That way PHP can handle all the redirecting... is this possible? Quote Link to comment https://forums.phpfreaks.com/topic/166856-mod-rewrite-how-should-i-set-it-up/#findComment-882506 Share on other sites More sharing options...
dreamwest Posted July 25, 2009 Share Posted July 25, 2009 No. Because your fake urls wont exist news/category/id-title will have to be real eg: news/category/id-title/index.php OK clear your htaccess file and add just this: Options +FollowSymlinks RewriteEngine on RewriteRule ^news/(.*)/(.*) index.php?type=news&category=$1&id=$2 [L,QSA] Your url will be : site.com/news/cat/ or site.com/news/cat/3 or site.com/news/anothercat/ or site.com/news/catanothercat/5 Let me know how it went Quote Link to comment https://forums.phpfreaks.com/topic/166856-mod-rewrite-how-should-i-set-it-up/#findComment-882509 Share on other sites More sharing options...
immanuelx2 Posted July 25, 2009 Author Share Posted July 25, 2009 No. Because your fake urls wont exist news/category/id-title will have to be real eg: news/category/id-title/index.php OK clear your htaccess file and add just this: Options +FollowSymlinks RewriteEngine on RewriteRule ^news/(.*)/(.*) index.php?type=news&category=$1&id=$2 [L,QSA] Your url will be : site.com/news/cat/ or site.com/news/cat/3 or site.com/news/anothercat/ or site.com/news/catanothercat/5 Let me know how it went Hmm... site.com/news/ does not work site.com/news/cat/ works site.com/news/cat/12053-the-article-title works Is there a separate line I should add for just site.com/news/ to work? Quote Link to comment https://forums.phpfreaks.com/topic/166856-mod-rewrite-how-should-i-set-it-up/#findComment-882635 Share on other sites More sharing options...
dreamwest Posted July 25, 2009 Share Posted July 25, 2009 site.com/news/ does not work I deliberately left this out so you can test the dynamic rule Now you just need the default ive changed it so it wont conflict with any other rule. #default for all sections -> http://site.com/type=news or http://site.com/type=forum RewriteRule ^type=(.*) index.php?type=$1 [L,QSA] Quote Link to comment https://forums.phpfreaks.com/topic/166856-mod-rewrite-how-should-i-set-it-up/#findComment-882869 Share on other sites More sharing options...
immanuelx2 Posted July 29, 2009 Author Share Posted July 29, 2009 site.com/news/ does not work I deliberately left this out so you can test the dynamic rule Now you just need the default ive changed it so it wont conflict with any other rule. #default for all sections -> http://site.com/type=news or http://site.com/type=forum RewriteRule ^type=(.*) index.php?type=$1 [L,QSA] I see... so no way to make it ^(.*)/ because it's the root and it can't be dynamic? I have to do ^news/, and any other sections, explicitly correct? Quote Link to comment https://forums.phpfreaks.com/topic/166856-mod-rewrite-how-should-i-set-it-up/#findComment-885570 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.