#1 2008-03-26 03:50:35
- Northie
- Moderator

- From: Yorkshire, UK
- Registered: 2006-08-19
- Posts: 2501
- I've been thanked 53 times.
-

.htaccess help
Hi guys, calling on your help now......
I need to get the requested url
/some-category/my-page/
to be interpreted by the server as
/index.php?category=some-category&page=my-page
and it must be done in htaccess
can anyone help?
Offline
#2 2008-03-26 06:06:37
- Ryan_steyn
- Member

- From: South Africa, Port Elizabeth
- Registered: 2006-08-23
- Posts: 1530
- I've been thanked 22 times.
-
Re: .htaccess help
hope this helps... http://corz.org/serv/tricks/htaccess2.php
Lalibela Game Reserve in malaria free South Africa
"Humans are by far the most fascinating creatures, in a universe with no boundaries and a world with so much unfound wonder we are the only entities capable of creating boredom"
Offline
#3 2008-03-26 10:01:48
- Steven_A_S
- Member

- From: San Antonio, TX
- Registered: 2006-08-07
- Posts: 403
- I've been thanked 16 times.
-
Re: .htaccess help
Hmm... looks interesting. Much easier than the custom 404 php page I set up to redirect such page links on binoware. Bookmarked
Offline
#4 2008-03-26 12:15:02
- Northie
- Moderator

- From: Yorkshire, UK
- Registered: 2006-08-19
- Posts: 2501
- I've been thanked 53 times.
-

Re: .htaccess help
Steven_A_S wrote:
Hmm... looks interesting. Much easier than the custom 404 php page I set up to redirect such page links on binoware. Bookmarked
Ha ha
That's the problem.......I have a custom 404 page that handles all requests, but on my new server it will only ever return a 404 status, even when i use PHP's
header("HTTP/1/1 200 OK");
so i want to get everything re-written to use the example i first stated
The next step, if I can't get my head round this .htaccess stuff is to try 301 - see if that comes back as expected....or not!
Offline
#5 2008-03-27 01:13:55
- Ryan_steyn
- Member

- From: South Africa, Port Elizabeth
- Registered: 2006-08-23
- Posts: 1530
- I've been thanked 22 times.
-
Re: .htaccess help
hang on a sec... your htaccess file may not be getting the right path to your error page... put the following in a php file and upload via ftp, go to that php page and it will echo the full directory path.
Code: php
<?
echo __FILE__;
?>
Thats just a shot in the dark though, i couldnt for the life of me get an htaccess redirect to work on the godaddy servers until i started using htaccess to password protect directories, then i found out about not having the correct directory path cause it couldnt find the password file... sneaky shtuff.
Lalibela Game Reserve in malaria free South Africa
"Humans are by far the most fascinating creatures, in a universe with no boundaries and a world with so much unfound wonder we are the only entities capable of creating boredom"
Offline
#6 2008-03-27 03:07:47
- Northie
- Moderator

- From: Yorkshire, UK
- Registered: 2006-08-19
- Posts: 2501
- I've been thanked 53 times.
-

Re: .htaccess help
Ryan_steyn wrote:
hang on a sec... your htaccess file may not be getting the right path to your error page... put the following in a php file and upload via ftp, go to that php page and it will echo the full directory path.
Code: php
<?
echo __FILE__;
?>
Thats just a shot in the dark though, i couldnt for the life of me get an htaccess redirect to work on the godaddy servers until i started using htaccess to password protect directories, then i found out about not having the correct directory path cause it couldnt find the password file... sneaky shtuff.
That won't help a bit
I know exactly what file is being called - 404.php.....beacuse i wouldn't see any content otherwise!!!!
The fact remains that I have several thousand pages (unique URLs) in google's index that are now all returning a 404 as well as delivering their unique content.
The server is sending a 404 because I have used a custom 404 page to handle all the requests.
Unlike my old server, I cannot overwrite this with a 200 OK if the requested page is valid, and content can be served
Offline
#7 2008-03-27 03:24:52
- Ryan_steyn
- Member

- From: South Africa, Port Elizabeth
- Registered: 2006-08-23
- Posts: 1530
- I've been thanked 22 times.
-
Re: .htaccess help
oi... that is a bit of a stuff up isnt it, well, im outa ideas...
Lalibela Game Reserve in malaria free South Africa
"Humans are by far the most fascinating creatures, in a universe with no boundaries and a world with so much unfound wonder we are the only entities capable of creating boredom"
Offline
#8 2008-03-27 08:07:17
- mobtex
- Member

- From: Loughborough, United Kingdom
- Registered: 2006-04-14
- Posts: 764
- I've been thanked 11 times.
-

Re: .htaccess help
RewriteEngine On
RewriteRule (.*)/(.*)-article.html /script.php?val=$1&val2=$2
Wouldn't something like that work? I know it's not the structure you wanted in the urls but the article.html ending will help the htaccess split that from any nested directory. Alright, needs tightening up for security as it's way to wide but it works 'in my head'.....
Put that at the top of your .htaccess and it will handle those requests before you do the 404 trapping. and should in theory return a http status of 200.
Andy Moore .mobi certified developer, web host and mobile media geek
NoSting Hosting - Brightside Hosting Ltd: Member of Nominet / eNom ETP
Ringtones - Palace Marketing Ltd: D2C and B2B mobile content since 2002
Offline
#9 2008-03-27 11:32:18
- Northie
- Moderator

- From: Yorkshire, UK
- Registered: 2006-08-19
- Posts: 2501
- I've been thanked 53 times.
-

Re: .htaccess help
mobtex wrote:
RewriteEngine On
RewriteRule (.*)/(.*)-article.html /script.php?val=$1&val2=$2
Thanks Andy - i'll give that a go
I just don't 'get' regex, and how to apply it to url rewriting; almost bought a book on regex today.........
Offline
#10 2008-03-27 11:58:16
- mobtex
- Member

- From: Loughborough, United Kingdom
- Registered: 2006-04-14
- Posts: 764
- I've been thanked 11 times.
-

Re: .htaccess help
save the book pennies for beer mate, get over to ilovejackdaniels.com they have a load of free and wicked cheat sheets including one or url rewriting that might be handy to you!
Andy Moore .mobi certified developer, web host and mobile media geek
NoSting Hosting - Brightside Hosting Ltd: Member of Nominet / eNom ETP
Ringtones - Palace Marketing Ltd: D2C and B2B mobile content since 2002
Offline
#11 2008-03-28 03:51:46
- Northie
- Moderator

- From: Yorkshire, UK
- Registered: 2006-08-19
- Posts: 2501
- I've been thanked 53 times.
-

Re: .htaccess help
That line has certainly helped - thanks
Offline
#12 2008-03-28 05:06:33
- mobtex
- Member

- From: Loughborough, United Kingdom
- Registered: 2006-04-14
- Posts: 764
- I've been thanked 11 times.
-

Re: .htaccess help
pleased to be of help to one of the most helpful people on here!
RewriteRule is voodoo good voodoo though!
Andy Moore .mobi certified developer, web host and mobile media geek
NoSting Hosting - Brightside Hosting Ltd: Member of Nominet / eNom ETP
Ringtones - Palace Marketing Ltd: D2C and B2B mobile content since 2002
Offline
#13 2008-04-07 09:08:13
- griffinsbridge
- Member

- From: York, England
- Registered: 2005-11-04
- Posts: 576
- I've been thanked 8 times.
-
Re: .htaccess help
I might be behind the times and Northie may have sorted his problem. Still, here's my answer.....................
The regex you need depends on what characters "some-category" and "my-page" consist of. For security reasons, you don't want to allow every possible character if only alphanumerics are used. Similarly, you don't want to allow alphas if only numerics are used.
So based on the example given:
Allow ALL characters:
Code:
RewriteRule ^/([^/])/([^/])/$ /index.php?category=$1&page=$2
Allow Alphanumeric only
Code:
RewriteRule ^/([a-zA-Z0-9]*)/([a-zA-Z0-9]*)/$ /index.php?category=$1&page=$2
Allow Alpha only (uppercase and lowercase)
Code:
RewriteRule ^/([a-zA-Z]*)/([a-zA-Z]*)/$ /index.php?category=$1&page=$2
Allow numeric only:
Code:
RewriteRule ^/([0-9]*)/([0-9]*)/$ /index.php?category=$1&page=$2
Allow pretty much anything:
Code:
RewriteRule ^/([^/])/([^/])/$ /index.php?category=$1&page=$2
Allow Alphanumeric and a + sign (for spaces)
Code:
RewriteRule ^/([a-zA-Z0-9+]*)/([a-zA-Z0-9+]*)/$ /index.php?category=$1&page=$2
From the above, I think a man of Northie's intellect could figure out everything he needs. For those without a brain the size of a small planet, here's a few tips...................
Each Regex in a line corresponds with a numbered variable. eg, the first ([a-zA-Z0-9]*) will correspond with $1 and so on. Remember that, it's very useful to know, ESPECIALLY if you're regexing something that will never be passed in the get string.
the asterix outside the square brackets denotes that ANY NUMBER of characters will be present in the rewritten URL. If you don't have that, then it'll only parse URLs with just one character in the variable and everything else will 404.
notice the $ after the rewritten URL's final forward slash?
That tells the server that everything should be from the root of the beginning of the rewritten URL (where the carat ^ is). So, if you have somesite.com/some-category/some-page/ it'll know to find index.php on somesite.com. It's not 100% nessecary, sometimes causes problems by being there, and sometimes causes problems by not being there. It's something you need to figure out yourself.
You MUST put a forward slash in the URLs on the page. somesite.com/some-category/some-page/ is not the same as somesite.com/some-category/some-page to a server (not even sure it's the same to Google tbh). if it's not there, you'll get a 404.
special characters need escaping with \. ie ([a-zA-Z0-9-]*) won't work but ([a-zA-Z0-9\-]*) should do.
a-zA-Z will do upper and lower case. a-z only lower and A-Z only upper. Make sure you allow both if both will be in the links.
Hows that?
Kudos for the link to corz by Ryan. Cor is a top geezer and deserves all the traffic he can get!
Last edited by griffinsbridge (2008-04-08 03:35:41)
So many pages, so little time.
Offline
#14 2008-04-07 10:52:57
- mobtex
- Member

- From: Loughborough, United Kingdom
- Registered: 2006-04-14
- Posts: 764
- I've been thanked 11 times.
-

Re: .htaccess help
Kudos for a sweet mini-guide!!!!
Andy Moore .mobi certified developer, web host and mobile media geek
NoSting Hosting - Brightside Hosting Ltd: Member of Nominet / eNom ETP
Ringtones - Palace Marketing Ltd: D2C and B2B mobile content since 2002
Offline
#15 2008-04-07 11:23:20
- griffinsbridge
- Member

- From: York, England
- Registered: 2005-11-04
- Posts: 576
- I've been thanked 8 times.
-
Re: .htaccess help
Cheers dude!
No point letting any knowledge die with me is there!! 
So many pages, so little time.
Offline
| Never |
- Sponsored Results
|
|