birwin
asked on
RewriteRule in .htaccess file not working
I am working on a site the uses
To change /content.php?page=XXX into something like http://mydomain.com/faqs
I tried adding some 301 redirects
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.
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.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) content.php?page=$1 [QSA,L]
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
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]
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
ASKER