Link to home
Start Free TrialLog in
Avatar of birwin
birwinFlag for Canada

asked on

RewriteRule in .htaccess file not working

I am working on a site the uses
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) content.php?page=$1 [QSA,L]

Open in new window


To change /content.php?page=XXX into something like http://mydomain.com/faqs

I tried adding some 301 redirects
Redirect 301 /oldfile.htm /newfile.htm

Open in new window

but the code above was appending a path to them that caused them to fail, give me 404s.

I did some research and found that a RewriteRule was a better choice, and it doesn't append the path, but it also doesn't work.

 RewriteRule "/privacy-policy$" "/privacy-statement"  [R=301,L]
           RewriteRule "/commercial-lending$" "/factoring"  [R=301,L]

Open in new window


But http://mydomain.com/commercial-lending just goes to that exact url, which fails. It is ignoring the redirect.

I am just hacking at these .htaccess statements, and I'd appreciate some guidance on how to get some form of redirect to work that is compatible with my existing code. I have about 30 redirects.

Thanks.
SOLUTION
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of birwin

ASKER

It was showing https://www.mydomain.com/factoring?page=commercial-lending
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of birwin

ASKER

Dave, my "pretty urls" was working fine before I added the redirects. But the code that generates the "pretty urls" is blowing up the redirects.

I started with "Redirect 301 /oldfile.htm /newfile.htm" but that gave me urls like https://www.mydomain.com/factoring?page=commercial-lending

I then tried the RewriteRule approach, which stopped appending the path, but it just doesn't do the redirect, or at least as I have it currently written.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of birwin

ASKER

So the answer was to place the rewrites first, and terminate them with [R=301,L]

Then I ran:

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) content.php?page=$1 [QSA,L]

below them.

This caused a problem with the home page, as it generated a 404. So I added the following to the top of content.php.
if(!$page){
header("Location: https://mysite.com/index");
}

I am still looking for a "pure" .htaccess solution, but this now works well.

So no one came close, but apparently I have to award points, so I will award them equally.