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

Link to comment
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

 

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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] 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.