Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

RewriteRule in .htaccess file not working

Posted on 2016-09-10
8
Medium Priority
?
70 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 1000 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 59

Assisted Solution

by:Julian Hansen
Julian Hansen earned 1000 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
It was showing https://www.mydomain.com/factoring?page=commercial-lending
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 59

Assisted Solution

by:Julian Hansen
Julian Hansen earned 1000 total points
ID: 41792307
Based on what rule?
0
 
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 59

Assisted Solution

by:Julian Hansen
Julian Hansen earned 1000 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 84

Accepted Solution

by:
Dave Baldwin earned 1000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

610 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