Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    21
    Thanks
    3
    Thanked 2 Times in 2 Posts

    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?

  • #2
    New Coder
    Join Date
    Mar 2009
    Posts
    21
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Now, now, don't you all chime in at once... where is an expert when you need one?

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    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.
    Last edited by abduraooft; 03-27-2009 at 09:49 AM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #4
    New Coder
    Join Date
    Mar 2009
    Posts
    21
    Thanks
    3
    Thanked 2 Times in 2 Posts
    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.

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    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.
    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?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #6
    New Coder
    Join Date
    Mar 2009
    Posts
    21
    Thanks
    3
    Thanked 2 Times in 2 Posts
    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.

  • #7
    Regular Coder
    Join Date
    Dec 2008
    Location
    Tannhäuser Gate
    Posts
    286
    Thanks
    7
    Thanked 58 Times in 57 Posts
    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.

  • #8
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,695
    Thanks
    6
    Thanked 1,011 Times in 984 Posts
    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>

  • #9
    New Coder
    Join Date
    Mar 2009
    Posts
    21
    Thanks
    3
    Thanked 2 Times in 2 Posts
    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.

  • #10
    Regular Coder
    Join Date
    Dec 2008
    Location
    Tannhäuser Gate
    Posts
    286
    Thanks
    7
    Thanked 58 Times in 57 Posts
    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

  • Users who have thanked freedom_razor for this post:

    levicki (03-29-2009)

  • #11
    New Coder
    Join Date
    Mar 2009
    Posts
    21
    Thanks
    3
    Thanked 2 Times in 2 Posts

    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •