...

View Full Version : no-www and pretty urls together, how?



levicki
03-26-2009, 09:02 PM
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:


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:


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
03-27-2009, 04:37 AM
Now, now, don't you all chime in at once... where is an expert when you need one?

abduraooft
03-27-2009, 09:43 AM
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

RewriteCond %{REQUEST_URI} ^/articles/category/.*
RewriteRule ^(.*)$ serve.php?page=$1 [L]
and then in my serve.php file, I'll have

$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

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.

levicki
03-27-2009, 01:43 PM
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.

abduraooft
03-27-2009, 01:44 PM
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.

Basically the problem I have is how to properly merge two rewriting rules be it in the same /.htaccess or in two separate ones.


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?

levicki
03-28-2009, 08:40 AM
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/category/yyyy/mm/dd/file.php

no-www rule catches it and rewrites it to:

http://example.com/articles/category/yyyy/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.php?name=category&date=yyyymmdd&file=file.php

Does not run.

freedom_razor
03-28-2009, 01:12 PM
That's in .htaccess in a directory above articles [so in www.example.com following your examples]


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.

the rule that should have rewritten it to:

http://example.com/articles/serve.php?name=category&date=yyyymmdd&file=file.php

VIPStephan
03-28-2009, 04:00 PM
I’m also not at all an htaccess expert but CMS Made Simple (http://cmsmadesimple.org), 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):



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


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

levicki
03-28-2009, 08:05 PM
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.

freedom_razor
03-28-2009, 08:26 PM
What I posted above doesn't work?

.htaccess

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

levicki
03-29-2009, 01:01 AM
Sorry for the file .vs. filename confusion, in the serve.php query string it should be filename so you got it right.


What I posted above doesn't work?

.htaccess

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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum