Go Back   CodingForums.com > :: Server side development > Apache configuration

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-26-2009, 09:02 PM   PM User | #1
levicki
New Coder

 
Join Date: Mar 2009
Posts: 18
Thanks: 3
Thanked 2 Times in 2 Posts
levicki is on a distinguished road
Cool no-www and pretty urls together, how?

I am trying to figure this one out but since I can't enable rewrite logging to try to understand what I am doing wrong I have to ask the experts here:

Basically, everyone gives an example for no-www rewrite similar to this one:
Code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
That goes into .htaccess in / folder and it works fine

Now, lets say that I have /articles/category/yyyy/mm/dd/article.php path:

How do I rewrite that to /articles/serve.php?name=category&date=yyyymmdd&file=article.php?

I tried adding this to .htaccess:
Code:
RewriteRule ^/articles/([a-z]*)/([0-9][0-9][0-9][0-9])/([0-9][0-9])/([0-9][0-9])/(.+\.php)$	/articles/serve.php?name=$1&date=$2$3$4&filename=$5
But it doesn't work. Server ignores the rule and just serves /articles/category/yyyy/mm/dd/article.php file which does exist but it should be served included from serve.php (because of navigation, layout, etc).

Before trying this I was using local .htaccess in /articles directory but then I had two other issues:

1. no-www rewrite didn't work for URLs like http://www.example.com/articles/category/...

OR:

2. If I enabled RewriteOptions inherit then no-www rewrite worked but I got /articles stripped out of the path and was getting 404 because of that.

So, what am I doing wrong here? How to have both things working at once?
levicki is offline   Reply With Quote
Old 03-27-2009, 04:37 AM   PM User | #2
levicki
New Coder

 
Join Date: Mar 2009
Posts: 18
Thanks: 3
Thanked 2 Times in 2 Posts
levicki is on a distinguished road
Now, now, don't you all chime in at once... where is an expert when you need one?
levicki is offline   Reply With Quote
Old 03-27-2009, 09:43 AM   PM User | #3
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
I'm not an htaccess expert, so when making a custom framework, I use a mix of simple htaccess rules and some tricks with the $_SERVER['REQUEST_URI'] variable.

ie, I'd do it like
Code:
RewriteCond %{REQUEST_URI} ^/articles/category/.*
RewriteRule ^(.*)$ serve.php?page=$1 [L]
and then in my serve.php file, I'll have
PHP Code:
$req=explode('/'$_SERVER['REQUEST_URI']);
//print_r($req); 
Thus, all the required parameters will be inside the $req array, and I can make the conditions/decisions based on those values. I don't consider this as a good method as I should take care of the order of variables in the url. The advantage(from my experience) of this rule is, I'll get the execution control in the desired file, even if there is some missing slashes or parameters in the requested url (which helps me to make a custom "did you mean" option). Further, this simple rules won't interfere with any other rules like (no-www) in any case.

Just wait for a better solution, if the above is not satisfactory.
PS: After all, I haven't used more than one serving files yet, so I use
Code:
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php?page=$1 [L]
and write all the processing codes inside my index.php file.
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

Last edited by abduraooft; 03-27-2009 at 09:49 AM..
abduraooft is offline   Reply With Quote
Old 03-27-2009, 01:43 PM   PM User | #4
levicki
New Coder

 
Join Date: Mar 2009
Posts: 18
Thanks: 3
Thanked 2 Times in 2 Posts
levicki is on a distinguished road
Thanks for replying but that still doesn't help me with no-www part which has to be done on .htaccess level.

Basically the problem I have is how to properly merge two rewriting rules be it in the same /.htaccess or in two separate ones.
levicki is offline   Reply With Quote
Old 03-27-2009, 01:44 PM   PM User | #5
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,678
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Quote:
Thanks for replying but that still doesn't help me with no-www part which has to be done on .htaccess level.
As I said above, the rules/approach won't interfere with your no-www rule, so you can keep it as such.
Quote:
Basically the problem I have is how to properly merge two rewriting rules be it in the same /.htaccess or in two separate ones.
Code:
RewriteEngine On
#rules to make it no-www
#next set of rules, as per the priority
..........
#next set of rules, as per the priority
What's the issue of writing them all in the same file?
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 03-28-2009, 08:40 AM   PM User | #6
levicki
New Coder

 
Join Date: Mar 2009
Posts: 18
Thanks: 3
Thanked 2 Times in 2 Posts
levicki is on a distinguished road
If I knew what the problem is I wouldn't ask, I really don't know, they refuse to work together for some reason.

For example, when server gets:

http://www.example.com/articles/cate...mm/dd/file.php

no-www rule catches it and rewrites it to:

http://example.com/articles/category...mm/dd/file.php

Since the file is actually there it gets served as a raw file and the rule that should have rewritten it to:

http://example.com/articles/serve.ph...&file=file.php

Does not run.

Last edited by levicki; 03-28-2009 at 08:46 AM..
levicki is offline   Reply With Quote
Old 03-28-2009, 01:12 PM   PM User | #7
freedom_razor
Regular Coder

 
Join Date: Dec 2008
Location: Tannhäuser Gate
Posts: 286
Thanks: 7
Thanked 58 Times in 57 Posts
freedom_razor is an unknown quantity at this point
That's in .htaccess in a directory above articles [so in www.example.com following your examples]
Code:
RewriteRule ^articles/([a-z]+)/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.+\.php)$	articles/serve.php?name=$1&date=$2$3$4&filename=$5
Also, in your rule there's filename but in the links you post it is file. Don't know which one your serve.php is expecting, I left filename.
Quote:
Originally Posted by levicki View Post
the rule that should have rewritten it to:
Code:
http://example.com/articles/serve.php?name=category&date=yyyymmdd&file=file.php

Last edited by freedom_razor; 03-28-2009 at 01:29 PM..
freedom_razor is offline   Reply With Quote
Old 03-28-2009, 04:00 PM   PM User | #8
VIPStephan
The fat guy next door


 
VIPStephan's Avatar
 
Join Date: Jan 2006
Location: Halle (Saale), Germany
Posts: 7,587
Thanks: 5
Thanked 864 Times in 841 Posts
VIPStephan is a jewel in the roughVIPStephan is a jewel in the roughVIPStephan is a jewel in the rough
I’m also not at all an htaccess expert but CMS Made Simple, with which I’m working frequently, has a default htaccess for pretty URLs with the following content (the rule at the bottom is for the actual URL rewrite):

Code:
<IfModule mod_rewrite.c>
RewriteEngine on
#
#Sub-dir e.g: /cmsms/
RewriteBase /
#
# 301 Redirect all requests that don't contain a dot or trailing slash to
# include a trailing slash
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]
# Rewrites urls in the form of /parent/child/
# but only rewrites if the requested URL is not a file or directory
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
</IfModule>
And I just include the no-www rule after the RewriteBase command (or whatever it’s called) and before the other rewrite conditions so the outcome is this:
Code:
<IfModule mod_rewrite.c>
RewriteEngine on
#
#Sub-dir e.g: /cmsms/
RewriteBase /
#
# no www rewrite rule
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
#
# 301 Redirect all requests that don't contain a dot or trailing slash to
# include a trailing slash
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]
# Rewrites urls in the form of /parent/child/
# but only rewrites if the requested URL is not a file or directory
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
</IfModule>
__________________
Don’t click this link!
VIPStephan is offline   Reply With Quote
Old 03-28-2009, 08:05 PM   PM User | #9
levicki
New Coder

 
Join Date: Mar 2009
Posts: 18
Thanks: 3
Thanked 2 Times in 2 Posts
levicki is on a distinguished road
I see. That is similar to what I was trying to do, only with a quirk.

On my server actual files requested do exist but they need to be rewritten because they are just the articles with the basic formatting -- no header, footer, or navigation. They get served by serve.php which constructs the actual page (keywords, title, xhtml, navigation, etc) from mysql database and the file requested.

The fact that I can't figure out the proper .htaccess really puzzles me.
levicki is offline   Reply With Quote
Old 03-28-2009, 08:26 PM   PM User | #10
freedom_razor
Regular Coder

 
Join Date: Dec 2008
Location: Tannhäuser Gate
Posts: 286
Thanks: 7
Thanked 58 Times in 57 Posts
freedom_razor is an unknown quantity at this point
What I posted above doesn't work?

.htaccess
Code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
RewriteRule ^articles/([a-z]+)/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.+\.php)$	articles/serve.php?name=$1&date=$2$3$4&filename=$5

Last edited by freedom_razor; 03-28-2009 at 09:08 PM.. Reason: Added whole .htaccess code
freedom_razor is offline   Reply With Quote
Users who have thanked freedom_razor for this post:
levicki (03-29-2009)
Old 03-29-2009, 01:01 AM   PM User | #11
levicki
New Coder

 
Join Date: Mar 2009
Posts: 18
Thanks: 3
Thanked 2 Times in 2 Posts
levicki is on a distinguished road
Cool

Sorry for the file .vs. filename confusion, in the serve.php query string it should be filename so you got it right.

Quote:
Originally Posted by freedom_razor View Post
What I posted above doesn't work?

.htaccess
Code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
RewriteRule ^articles/([a-z]+)/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.+\.php)$	articles/serve.php?name=$1&date=$2$3$4&filename=$5
It works now, thanks!

I must have been making a mistake somewhere which I couldn't see on my own.

Last edited by levicki; 03-29-2009 at 11:03 AM..
levicki is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:20 AM.


Advertisement
Log in to turn off these ads.