Forum Moderators: phranque
This has been driving me mad for 3 days...
I'm currently using this in the root .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule 2009/([^/]+)/([^/]+)/?$ 2009.php?m=$1&t=$2 [L]
RewriteRule archive/([^/]+)/([^/]+)/?$ archive.php?y=$1&m=$2 [L]
RewriteRule ^(.*)/$ /$1.php [L]
This works o.k except I need to create a PHP page for any pages in the top folder.
I want to use something like this:
$t = $blogobj->somedataback($_GET['t']);
$sql = "SELECT * FROM $resourcetable WHERE urltitle='$t'";
In the root file index.php?t=page-title
This is working when using the query string, but I can't get the redirect to send domain/page-title/ to domain/index.php?t=page-title.
I would like to keep the 2009 and archive redirect, but I suppose the other would be redundant.
Can anyone help?
Thanks
RewriteRule ^(.*)/$ /$1.php [L] This rewrite accepts a *URL* like /thisname/ and rewrites this to get content from the *file* /thisname.php
It also accepts a *URL* request request like /folder/folder/folder/ and rewrites this to get content from the *file* /folder/folder/folder.php
For extensionless URLs I would *not* end the URL with a trailing slash. A trailing slash denotes a physical folder.
For your request, something like this might be useful:
RewriteRule ^([^/]+)$ /index.php?t=$1 [L] Ahead of all your rewrites, make sure you also redirect non-canonical URL requests to their canonical form.
I thought there might be a problem with the trailing slash...I have tried this and the site gives a Internal Server Error. How would I track down what is causing this?
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.+/)?index\.php\ HTTP
RewriteRule ^(.+/)?index\.php$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^([^/]+)$ /index.php?t=$1 [L]
RewriteRule 2009/([^/]+)/([^/]+)/?$ 2009.php?m=$1&t=$2 [L]
RewriteRule archive/([^/]+)/([^/]+)/?$ archive.php?y=$1&m=$2 [L]
I tried just using:
RewriteRule ^([^/]+)$ /index.php?t=$1 [L]
On it's own but that also gave the same error.
[edited by: jdMorgan at 1:20 am (utc) on June 16, 2009]
[edit reason] example.com [/edit]
domain/2009.php?m=06&y=2009 seems to be redirecting to index.php?t=2009.php, but receives no values for 'm'
and domain/archive.php?&y=2009&m=06 appears to be redirecting to index.php?t=archive.php, and receive no value for 'm' or 'y.
I am also using domain/getimage?id=123 for displaying blob images, but again it appears to be redirecting to index.php?t=getimage.php
Is this what you expect should happen? Do I need to move getimage.php, 2009.php and archive.php to another directory?
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.+/)?index\.php\ HTTP
RewriteRule ^(.+/)?index\.php$ http://www.example.co.uk/$1 [R=301,L]
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.co.uk [NC]
RewriteRule (.*) http://www.example.co.uk/$1 [R=301,L]
RewriteCond $1 !index\.php$
RewriteRule ([^/]+)$ index.php?t=$1 [L]
In the folders domain/2009/ and domain/archive/ I'm not sure if it's o.k to do this...Ideally I would like to keep the getimage.php, archive.php and 2009.php files in the root.
So, Would I need this:
RewriteRule ^([^/]+)$ /index.php?t=$1 [L]
In the root file, with some kind of reg expressions in the brackets to prevent these 3 pages from being redirected to index.php ?
[edited by: jdMorgan at 12:26 pm (utc) on June 16, 2009]
[edit reason] examplified [/edit]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.+/)?index\.php\ HTTP
RewriteRule ^(.+/)?index\.php$ http://www.example.co.uk/$1 [R=301,L]
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.domain\.co.uk [NC]
RewriteRule (.*) http://www.example.co.uk/$1 [R=301,L]
RewriteCond $1 !^index\.php$
RewriteRule ^([^/]+)$ /index.php?t=$1 [L]
rewritecond %{REQUEST_URI} ^/2009/
RewriteRule (.*) /2009.php?m=$1&t=$2 [L,QSA]
rewritecond %{REQUEST_URI} ^/getimage/
RewriteRule (.*) /getimage.php?id=$1 [L,QSA]
rewritecond %{REQUEST_URI} ^/archive/
RewriteRule (.*) /archive.php?y=$1&m=$2 [L,QSA]
But this doesn't seem to make any difference, oops sorry about URLs
[edited by: jdMorgan at 12:27 pm (utc) on June 16, 2009]
[edit reason] example.co.uk [/edit]
Realize that mod_rewrite in .htaccess is recursive (as illustrated by the previous looping problem), and that "2009/m/y" will get rewritten first to 2009.php, and then by another rule to index.php, unless you explicitly stop that.
There are two ways to do this. The first is to explicitly prevent it by listing exclusions in a RewriteCond so that 2009/ and getimage/, etc. are not rewritten to index.php. This is a specific method, and will require maintenance over time if you add more "pseudo-directories referring to scripts" to the site.
A more general solution (with possible far-ranging effects) is to exclude paths containing a period from being rewritten to index.php. This will stop not only 2009.php from being re-rewritten, but also prevent requests for all of your images, scripts, CSS files, etc. from being rewritten to index.php. It's a simple matter of changing "[^/]+" to "[^./]+", but is that what you want?
A third solution would be to stop paths ending in ".php" from being rewritten to index.php.
Note also that your coding can be simplified. For example,
RewriteCond %{REQUEST_URI} ^/archive/
RewriteRule (.*) /archive.php?y=$1&m=$2 [L,QSA]
RewriteRule archive/(.*)$ /archive.php?y=$1&m=$2 [QSA,L]
But look at either incarnation of that that rule... $2 is undefined, leaving the m= value blank.
For the sake of faster problem resolution and clear discussion, let's ignore all but one of the 'special' subdirectories for now, and omit both the index and domain canonicalization redirects. Divide and conquer, as it were... It is much easier to debug two rules than to debug ten, and the solution for "getimage" will be the same as that for "2009".
Jim
[edited by: jdMorgan at 12:58 pm (utc) on June 16, 2009]
I have decided to exclude paths containing a period from being rewritten to index.php for now. This is working great, I will try the other ideas at some point if necessary, or I feel like taxing my brain to breaking point.
I don't want to take much more of your time up on this, but wondered if you could expand a little on the "possible far-ranging effects" of excluding URLs with periods being rewritten. My 'user' URLs are stripped of any non-alphanum chars, but I suppose there might be external factors to consider that I am unaware of.
Thanks again, Tom
A major problem with "learning" mod_rewrite has nothing to do with the module directives or the intricacies of regular-expressions patterns. It has to do with looking at a pattern, recognizing what it matches in terms of characters, and then thinking, "What URLs will this pattern match, and what does that mean with regard to the operation of my site?" While we here can discuss mod_rewrite directives and patterns all day, only you can know what your entire "URL-space" looks like, and what it "means" when (as in this example) we exclude a whole "class" of URLs from a rule.
So there's nothing mysterious here, but no URLs containing a slash or a period will be rewritten by that rule with that pattern.
As an example of this "what does it mean" thing, consider since "index.php" contains a period, it will no longer be necessary to provide a specific RewriteCond exception for that URL-path to prevent looping, and therefore
RewriteCond $1 !^index\.php$
RewriteRule ^([^./]+)$ /index.php?t=$1 [L]
RewriteRule ^([^./]+)$ /index.php?t=$1 [L]
Jim
Understanding the "URL space" that the content, images, CSS and JS files, robots.txt, SE account verification files, and so on, all occupy, is a vital step in coding this stuff.
I have made many errors over the years. Beyond simple typos (hundreds), most of the others were where the code I wrote did what I wanted for some URL requests but did something else (unwanted and unforeseen) with other URL requests.
As Jim has said many times over the years "Error 500? Only 499 to go!". :)