Jump to content

Mod Rewrite (how should I set it up?)


immanuelx2

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/166856-mod-rewrite-how-should-i-set-it-up/
Share on other sites

 

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

 

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?

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

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?

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

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.

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
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

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

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
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

  Quote
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?

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.