View Full Version : .htaccess RewriteRules help
yagran
Sep 24, 2008, 08:05 AM
Hi there i'm trying to achieve a rewrite so that i can link to pages on my website easier.
here's what i currently have in a .htaccess file in the httpdocs file on my hosting:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^page/home$ ?page=home.html
RewriteRule ^page/blog$ ?page=blogcms/blog.php
RewriteRule ^page/lineup$ ?page=lineup.html
RewriteRule ^page/djs$ ?page=djs.html
I cant really see whats wrong with it? but im such a novice at this that im not sure if its something in this file thats wrong, or if its something server setup related...?
thanks for any advice
mcnicks
Sep 24, 2008, 08:08 AM
Have a look at the RewriteBase directive. When you put RewriteRules in .htaccess files, you have to define the directory that the rules apply to.
angelwatt
Sep 24, 2008, 08:24 AM
Have a look at the RewriteBase directive. When you put RewriteRules in .htaccess files, you have to define the directory that the rules apply to.
Actually I don't think the RewriteBase part is needed if they are applying it to root, which I think is what he's doing, though could be wrong.
To yagran, what URLs are you typing in to test these rewrite rules, and what are you expecting the URL to change to? Just want to make sure it's clear.
yagran
Sep 24, 2008, 09:16 AM
thanks for replying.
Inwould expect that http://radiofluff.co.uk/page/djs would redirect to http://radiofluff.co.uk/?page=djs.html
angelwatt
Sep 24, 2008, 09:31 AM
thanks for replying.
Inwould expect that http://radiofluff.co.uk/page/djs would redirect to http://radiofluff.co.uk/?page=djs.html
Try adding a [L] to the end of each line and/or remove the ^ on each line:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^page/home$ ?page=home.html [L]
RewriteRule ^page/blog$ ?page=blogcms/blog.php [L]
RewriteRule ^page/lineup$ ?page=lineup.html [L]
RewriteRule ^page/djs$ ?page=djs.html [L]
yagran
Sep 24, 2008, 10:00 AM
Try adding a [L] to the end of each line and/or remove the ^ on each line:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^page/home$ ?page=home.html [L]
RewriteRule ^page/blog$ ?page=blogcms/blog.php [L]
RewriteRule ^page/lineup$ ?page=lineup.html [L]
RewriteRule ^page/djs$ ?page=djs.html [L]
Thanks, but it still doesnt work :(
angelwatt
Sep 24, 2008, 10:38 AM
Well figured it out. You can't use .htaccess files because your server isn't Apache, it's Windows IIS, so you'll have to find a different solution that works with that.
stndn
Sep 24, 2008, 12:03 PM
How did it not work?
Did you get page not found, or your file simply didn't output the page name as defined by .htaccess? Actually, do you want it to load the contents of page=djs.html, or do you want the page to actually change to ?page=djs.html
Here I'm assuming the former. If it's the later, add [ R ] for redirect and make the rules in the examples become [ R, L ].
I tried the code you posted and it worked just fine. FYI, the ^ in the rewrite rule simply marks the beginning of URL (so ^page means anything that begins with page).
As for suggestion by angelwatt, having the [ L ] at each rule means do not continue processing the rest of the rules on the first match.
In any case, could you tell me what you get by going to http://radiofluff.co.uk/?page=djs.html ? By default, it usually opens up index.html, index.php, or any other index.* files, and not djs.html.
If what you want to achieve is that for people who opens http://radiofluff.co.uk/page/djs should get the file djs.html instead, try this rule:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^page/home$ home.html [L]
RewriteRule ^page/blog$ blogcms/blog.php [L]
RewriteRule ^page/lineup$ lineup.html [L]
RewriteRule ^page/djs$ djs.html [L]
If you want them to load a specific file (for example, index.php) and pass the page as a parameter to $_GET, simply attach index.php to it:
RewriteRule ^page/home$ index.php?page=home.html [ L ]
FYI, this is what I used for testing, saving the file as index.php:
<?php
if (isset ($_GET['page'])) { print "<p>page is {$_GET['page']}</p>"; }
else { print "<p>no page</p>"; }
/* contents of .htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^page/blah$ ?page=blah.html
RewriteRule ^page/huh$ ?page=huh.html
*/
?>
-stndn.
Edit: Whoopss.. just read the post by angelwatt. I guess this post won't do much good, then...
yagran
Sep 24, 2008, 08:15 PM
Well figured it out. You can't use .htaccess files because your server isn't Apache, it's Windows IIS, so you'll have to find a different solution that works with that.
I have the ISAPI equivilent of mod_rewrite installed....
It should handle .htaccess files the same way apache would.
angelwatt
Sep 24, 2008, 08:55 PM
I have the ISAPI equivilent of mod_rewrite installed....
It should handle .htaccess files the same way apache would.
Based on the documentation (http://www.isapirewrite.com/docs/#conffile) for ISAPI I think you need to place the code inside the httpd.ini file rather than creating a .htaccess file. Their documentation isn't exactly wonderful though, so I may be wrong.
vBulletin® v3.6.10, Copyright ©2000-2009, Jelsoft Enterprises Ltd.