?
Solved

RewriteRule in .htaccess file not working

Posted on 2016-09-10
8
Medium Priority
?
62 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 58

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
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

 
LVL 58

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 58

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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
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…
Suggested Courses

762 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