homepage Welcome to WebmasterWorld Guest from 54.204.141.129
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
domain/page-title redirection
tomhumf




msg:3933474
 12:46 pm on Jun 15, 2009 (gmt 0)

Hi,

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

 

g1smd




msg:3933484
 12:59 pm on Jun 15, 2009 (gmt 0)

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.

tomhumf




msg:3933585
 3:23 pm on Jun 15, 2009 (gmt 0)

Thanks,

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]

jdMorgan




msg:3933761
 7:25 pm on Jun 15, 2009 (gmt 0)

In your third rule, you need to explicitly prevent index.php from being rewritten to itself:

RewriteCond $1 !^index\.php$
RewriteRule ^([^/]+)$ /index.php?t=$1 [L]

When you get a server error, take a look at your server error log file -- It is often quite helpful in solving problems.

Jim

tomhumf




msg:3934091
 6:42 am on Jun 16, 2009 (gmt 0)

Thanks jdMorgan, There are no server errors now and the root redirects are working now, i.e, domain/contact is redirecting to index.php?t=contact

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?

tomhumf




msg:3934124
 7:55 am on Jun 16, 2009 (gmt 0)

O.k, getimage is working when moved to a lower directory, the others are working by using:

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]

tomhumf




msg:3934170
 9:23 am on Jun 16, 2009 (gmt 0)

I thought this might be similar to what I need [webmasterworld.com...]
I have tried doing:
Options +FollowSymLinks
RewriteEngine on

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]

jdMorgan




msg:3934307
 12:58 pm on Jun 16, 2009 (gmt 0)

Everything here depends on what you *want* to do.
For example, you stated several posts ago that "/2009.php?m=06&y=2009 seems to be redirecting to index.php?t=2009.php, but receives no values for 'm'". Leaving aside the missing parameters, is it the case that you want to rewrite "2009.php" to index.php, or do you want to stop after 2009/m/y is rewritten to 2009.php?m=m&y=y?

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]

reduces to

RewriteRule archive/(.*)$ /archive.php?y=$1&m=$2 [QSA,L]

with no change whatsoever in function.

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]

tomhumf




msg:3935052
 11:37 am on Jun 17, 2009 (gmt 0)

Thanks Jim, I'm eternally grateful for your help.

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

jdMorgan




msg:3935106
 12:50 pm on Jun 17, 2009 (gmt 0)

The far-ranging effects are just that: A lot of requested URLs will contain a period, and all of them will be excluded from being rewritten to your script.

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]

reduces to
RewriteRule ^([^./]+)$ /index.php?t=$1 [L]

Also, as implied by the example in my previous post, a separate RewriteCond examining REQUEST_URI is never needed if the RewriteRule pattern can be used to match the pattern. This means that all or most of your other rules don't need a RewriteCond, because the "directory" can be tested by the RewriteRule itself.

Jim

g1smd




msg:3935583
 11:27 pm on Jun 17, 2009 (gmt 0)

There's no substitute for getting dirty with the code on your test server. There's a number of gotchas that will get you - and as every server and website is different, many of them might not be entirely obvious the first time you drop the code in.

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!". :)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved