Solved

RewriteRule in .htaccess file not working

Posted on 2016-09-10
8
54 Views
Last Modified: 2016-09-13
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.
0
Comment
Question by:birwin
  • 3
  • 3
  • 2
8 Comments
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
ID: 41792285
'RewriteRule' is the destination and must be a real file.  It is Not what the address seen in the browser (though it can be).

Here is probably the info you want:  http://code.tutsplus.com/tutorials/using-htaccess-files-for-pretty-urls--net-6049  Note that if your 'pretty URL' is an actual directory name then that is where it will go and will Not follow the rewrite rule.
0
 
LVL 52

Assisted Solution

by:Julian Hansen
Julian Hansen earned 250 total points
ID: 41792286
but the code above was appending a path to them that caused them to fail, give me 404s
Can you give an example?
0
 
LVL 6

Author Comment

by:birwin
ID: 41792299
0
 
LVL 52

Assisted Solution

by:Julian Hansen
Julian Hansen earned 250 total points
ID: 41792307
Based on what rule?
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 6

Author Comment

by:birwin
ID: 41792308
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.
0
 
LVL 52

Assisted Solution

by:Julian Hansen
Julian Hansen earned 250 total points
ID: 41792322
I am not understanding how
Redirect 301 /oldfile.htm /newfile.htm
Can result in a url with factoring?page=commercial-lending
Are we missing a piece of the puzzle?
0
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 250 total points
ID: 41792517
When you have...
RewriteRule (.*) content.php?page=$1 [QSA,L]

Open in new window

You are going to end up on 'content.php'.  Then 'content.php' will read the query string ($1) to determine what to return to the browser.  'content.php' will not normally show up in the browser.  But since it is the target of the rule, I don't believe that you can further redirect it.
0
 
LVL 6

Author Comment

by:birwin
ID: 41797056
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.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now