SEF URL,s for CMS

Discuss practical ways rearrange URLs using mod_rewrite.

SEF URL,s for CMS

Postby RecoilUK » Thu Feb 19, 2009 3:18 am

Hi guys

I am designing a CMS that uses a slightly different approach to SEF URL,s than most by what I have seen.

Basically, instead of converting the SEF URL into a URL with an appended query string, my CMS will be using the SEF URL as is.

I seem to be having some problems designing a good set of mod rewrite rules for it.I can generally write a single rule and get it working just fine, its when I start combining them that things start going wrong.

I know how to describe these rules, so i,ll do that and maybe someone can help with coding them and help putting them in order.

    1 -- If there is a query string, it should be removed completely and visibly redirected.
    Code: Select all
    RewriteCond %{QUERY_STRING} !^$
    RewriteRule (.*) $1? [R=301]


    2 -- If the request is in the correct format i.e ...

    /index
    /index/
    /admin/modules
    /admin/modules/
    /news
    /news/
    /news/edit/123
    /news/edit/123/

    then am I correct in thinking that if the directories/files dont exist that the following should work ...
    Code: Select all
    RewriteCond ${REQUEST_FILENAME} !-f
    RewriteCond ${REQUEST_FILENAME} !-d
    RewriteRule .* index.php


    3 -- The directory structure of the cms is as follows ...

    /classes/
    /components/
    /modules/
    /templates/
    /images/
    .htaccess
    stylesheet.css
    index.php
    config.php

    All site requests will be going through index.php as this is the controller, if any requests are made for the files in the other directories, then this isnt allowed, and should be redirected to /index so they get the home page.

    I havnt got a clue how to do this, but I think I may need a .htaccess file in each directory, if someone could confirm this is it would be appreciated.

    4 -- For a page request, there will also be additional requests for other files, like .css, .ico, .gif etc, etc. Am I correct in thinking that these will have a referrer value correspondong to my site, therefore making it possible to create a rule that would allow these files to be served as is and not rediredted to index.php.

    5 -- Lastly, I would like a rule that strips off any file extensions like .php, .htm, .html and .asp, but everything I have tried results in an endless loop because i,m redirecting to index.php



Oh, one last question, i,m designing this CMS in PHP, will any of these rules affect php including files?

Errrr, thats about it, as you can see its getting complicated, and help anyone can give would be appreciated.

Thankyou
RecoilUK
 
Posts: 7
Joined: Wed Aug 06, 2008 5:36 pm

Postby richardk » Thu Feb 19, 2009 6:49 am

Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Block access to the following files and directories.
RewriteRule ^(\.htaccess|config\.php)$ /index? [R=301,L]
RewriteRule ^(classes|components|modules|templates)(/.*)?$ /index? [R=301,L]

# Strip off file extensions.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.(html?|php|asp)$ /$1? [R=301,L]

# Remove query strings.
RewriteCond %{QUERY_STRING} .
RewriteRule ^(.*)$ /$1? [R=301,L]

# Everything else to index.php
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule . /index.php [L]


2 -- If the request is in the correct format i.e ...

Will it only be letters and numbers?

3 -- The directory structure of the cms is as follows ...

/classes/
/components/
/modules/
/templates/
/images/
.htaccess
stylesheet.css
index.php
config.php

All site requests will be going through index.php as this is the controller, if any requests are made for the files in the other directories, then this isnt allowed, and should be redirected to /index so they get the home page.

If you are still designing the CMS you should think about moving these (the ones that don't need to be we accessible) outside the document root instead.

4 -- For a page request, there will also be additional requests for other files, like .css, .ico, .gif etc, etc. Am I correct in thinking that these will have a referrer value correspondong to my site, therefore making it possible to create a rule that would allow these files to be served as is and not rediredted to index.php.

You can't trust the Referer header.

i,m designing this CMS in PHP, will any of these rules affect php including files?

Mod_rewrite does not affect internal filesystem requests, only http://... etc. type requests.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby RecoilUK » Thu Feb 19, 2009 8:42 am

Wow

Thanks for the reply.

You nailed it first time, well nearly, but thats my fault.

Is there anyway I can alter the last RewriteRule to not process .css, .ico or anything else I can think of.

The reason being, for testing purposes, I have a php session counter on index.php which increments by 1 on each request, now if a favicon.ico or the css file doesnt exist, in some browsers it increments by more than one because its not finding the file, so redirects to /index.php.

If the files exist of course, then it works as expected and I thank you very much for the effort on helping me with this, it is very much appreciated.

Thanks again.
RecoilUK
 
Posts: 7
Joined: Wed Aug 06, 2008 5:36 pm

Postby RecoilUK » Thu Feb 19, 2009 8:47 am

Its ok, I think I have done it.

Code: Select all
# Everything else to index.php
RewriteCond %{REQUEST_URI} !.+(\.(css|ico))$
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule . /index.php [L]


Thanks again
RecoilUK
 
Posts: 7
Joined: Wed Aug 06, 2008 5:36 pm

Postby RecoilUK » Thu Feb 19, 2009 8:57 am

Or this way ...

Code: Select all
# Everything else to index.php
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule !.+(\.(css|ico))$ /index.php [L]


Should this way save additional processing time, as its only checking the first rule?

Oh, yes only letters and numbers for now, this may change to also allowing underscores also, and I could very easily move the files that dont require to be in the document root, thanks for the tip.

Thanks again
RecoilUK
 
Posts: 7
Joined: Wed Aug 06, 2008 5:36 pm

Postby richardk » Sat Feb 21, 2009 1:28 pm

Should this way save additional processing time, as its only checking the first rule?

Yes.

Oh, yes only letters and numbers for now, this may change to also allowing underscores also

Then using
Code: Select all
# Everything else to index.php
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^[a-z0-9/]+$ /index.php [L]

is another option. It won't match file extensions.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 25 guests

cron