Solved

RewriteRule in .htaccess file not working

Posted on 2016-09-10
8
50 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 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
Comment Utility
'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 51

Assisted Solution

by:Julian Hansen
Julian Hansen earned 250 total points
Comment Utility
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
Comment Utility
0
 
LVL 51

Assisted Solution

by:Julian Hansen
Julian Hansen earned 250 total points
Comment Utility
Based on what rule?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 6

Author Comment

by:birwin
Comment Utility
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 51

Assisted Solution

by:Julian Hansen
Julian Hansen earned 250 total points
Comment Utility
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 82

Accepted Solution

by:
Dave Baldwin earned 250 total points
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Accessibility and Usability are two concepts that seem to be closely related.  But, too many people seem to have a distorted perception of them. During last five years, those two words have come to the day-to-day work of almost every web develope…
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 demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

772 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

12 Experts available now in Live!

Get 1:1 Help Now