htaccess redirect with dynamic variables conflict - .htaccess

I'm working in an old CMS that uses htaccess to rewrite URIs with GET variables into something more user friendly.
RewriteEngine on
RewriteRule ^animals/(.*)/ secondary.php?page=$1
RewriteRule ^animals/(.*) secondary.php?page=$1
which results (correctly) in
http://www.example.com/animals/duck
The problem is I now need to redirect some of those pages to new pages. I've tried:
Redirect 301 /animals/goose http://www.example.com/animals/fowl
The redirect almost works, but it adds "?page=goose" to the end of the rewritten URI:
http://www.example.com/animals/fowl?page=goose
I've tried using RewriteRule as well as RewriteCond, but unfortunatley I'm having no luck. Any help would be immensely appreciated.

Try placing this before the other rules instead of the Redirect statement. R=301 is for the redirect and L signals that the rule in question is the last rule to be processed.
RewriteRule ^animals/goose /animals/fowl [R=301,L]
Also you can easily make the slash (just like any other character) optional with a question mark, instead of having two rules.
RewriteRule ^animals/(.*)/?$ secondary.php?page=$1

Related

.htaccess rewrite conflicting rules

I'm having an issue with some htaccess rules which I thought would be simple. I have some nice SEO friendly URL rewriting in place as below
RewriteEngine On
RewriteCond %{REQUEST_URI} !^(index\.php|/images|/templates|/views|/ajax|/uploads|/robots\.txt|/sitemap\.xml|/favicon\.ico|/scripts|/cron|/combine.php|/js|/css)
RewriteRule ^(.*)$ index.php?ref=$1&%{QUERY_STRING} [L]
This all works well and I want to keep this. I also wish to rewrite some old pages which Google WMT is reporting as 404's to the new equivalent and for that I'd like to use:
Redirect 301 /about_us http://example.com/about-us
The problem I have is that the URL that the browser is directed to is:
http://example.com/about-us?ref=about_us
The about_us is the old link and about-us is the correct link. If the htaccess redirected to example.com/about-us then the other SEO friendly rewrite rule will pick it up and show the page but eh extra ?ref= parameter is confusing it. I am guessing the two rules are conflicting to a degree but is there a way to get the two rules to work together e.g. redirect without the extra ?ref= parameter? My knowledge of htaccess is basic to say the least so I am a little stuck on this one.
Thanks in advance
Redirect and RedirectMatch are part of mod_alias, while the rewrite rules are part of mod_rewrite. The problem you're running into is when you mix the two, both modules affect the same request, thus two things happen when you only want one. In this case, you need to stick with just mod_rewrite and use this instead:
RewriteEngine On
RewriteRule ^about_us /about-us [L,R=301]
RewriteCond %{REQUEST_URI} !^(index\.php|/images|/templates|/views|/ajax|/uploads|/robots\.txt|/sitemap\.xml|/favicon\.ico|/scripts|/cron|/combine.php|/js|/css)
RewriteRule ^(.*)$ index.php?ref=$1&%{QUERY_STRING} [L]
Note that the rule that redirects comes before the rule that routes to index.php.

prefix a friendly url using mod rewrite in htaccess

Is it possible to prefix a htaccess rewrite rule
for example can a variable be used as a prefix to a url
website.com/$variable-for-sale/
/cupcakes-for-sale/
/pies-for-sale/
/flans-for-sale/
The idea is to then use that variable to display all the cupcakes/pies/flans for sale
How would this be written as a rewrite rule? Is it even possible?
Thanks
The first rule will take care of redirecting your ugly URL to Friendly like one.
The second rule will internally redirect it back so the browser URL remains the friendly URL while service the content of your page.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Redirect /?cake=anything to /anything-for-sale/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+\?cake=([^&\s]+) [NC]
RewriteRule ^ /%1-for-sale/? [R=302,L]
# Internally forward /anything-for-sale/ to /?cake=anything
RewriteRule ^([^-]+)-for-sale/?$ /?cake=$1 [NC,L]
Keep in mind I am using R=302 its always better to use 302 which means temporary redirect while testing a new rule before making it permanent as the permanent will cache the information to your browser. Once the rule is confirmed to be working as expected change R=302 to R=301.
To extract variable, you need to use regex parentheses in the correct pattern, then you can use $1 to fetch the group:
RewriteRule ^([^-]+)-for-sale/$ /target.php?variable=$1 [L]
The "target" part is the script you use to display the "variable". Since your question doesn't mention what that is, you have to figure it out.

What to do with Question Marks in HTAccess Redirects

I'm new to redirects and have researched how to do this but am just getting more and more confused.
What I'm wanting to do is create a rule to redirect:
From: http://www.example.com/wordpress/?p=1250
To: http://www.otherexample.com/blog
Where I'm running into issues is with the question mark. I've read somewhere that I'm not sure I'm doing this correctly, but here's what I have so far:
**Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^p=1297$
RewriteRule ^wordpress$ linktoothersite [R=301, QSA, L, NC]**
I'm failing somewhere, any of you see what I'm doing wrong? Any help would be appreciated. I could only post two links, so link to other site would be the link.
If it's a single link you want to change you don't need a htaccess rule for that you can just do a redirect.
redirect 301 /wordpress/?p=250 http://www.othersite.com/blog
If the URL parameter changes you can use this rule.
RewriteEngine on
RewriteRule ^wordpress/(.*) http://www.othersite.com/blog [R=301,QSA,NC,L]
Note that when you use the QSA flag it will append the URL with the query parameter so if that's what you want the resulting URL will be.
http://www.othersite.com/blog?p=250
Otherwise if you remove the QSA flag, then it will just redirect to this
http://www.othersite.com/blog

Need .htaccess assistance

We have an existing site, let's call it ourdomain.com. We moved content over from a site that is shutting down, let's call it legacydomain.com. We pointed the legacy domain at our server to the /public_html/ folder.
What I need is an .htaccess that will redirect legacydomain.com or legacydomain/anything-here to ourdomain.com/legacydomain/ with nothing else appended to the URL.
However, I also need a few specific URLs to redirect to certain destinations, and they don't really follow a pattern. For example:
legacydomain.com/something.html to ourdomain.com/legacydomain/something.html
legacydomain.com/another.html to ourdomain.com/legacydomain/folder/another.html
This is what I have tried:
RewriteCond %{HTTP_HOST} ^www\.legacydomain\.com$ [NC]
RewriteRule (.*) http://www.ourdomain.com/legacydomain/$1 [R=301,L]
Redirect 301 /something.html http://www.ourdomain.com/legacydomain/another.html
It mostly works, but if I visit legacydomain.com/anything-here it doesn't even attempt to rewrite, it just keeps the domain the same and gives a 404. And also I have a feeling that even if it did work, something like legacydomain.com/anything-here/more-stuff would get rewritten as ourdomain.com/legacydomain/anything-here/more-stuff which I don't want.
Only other thing in the .htaccess is rewriting non-www to www, and the standard WordPress stuff. Any guidance would be greatly appreciated. Everything above should have an http:// and www in front for the examples, but it wouldn't let me post that many "links".
For each specific rewrite you would need two lines, as follows. Depending on your existing config you may need to add a slash at the beginning of the RewriteRule in front of something.html if this doesn't work.
RewriteCond %{HTTP_HOST} legacydomain.com
RewriteRule something.html http://ourdomain.com/legacydomain/something.html [R=301,L]
Then you would use a catch-all for everything else.
RewriteCond %{HTTP_HOST} legacydomain.com
RewriteRule (.*) http://ourdomain.com/legacydomain/ [R=301,L]
Personally, I would go for the simplest solution which doesn't use mod_rewrite. First, just redirect the specific pages to wherever they need to go.
Redirect 301 /something.html http://ourdomain.com/legacydomain/something.html
Redirect 301 /another.html http://ourdomain.com/legacydomain/another.html
Then, simply redirect everything else to the base URL.
RedirectMatch 301 (.*) http://ourdomain.com/legacydomain/
These must be put in your .htaccess file before the RewriteEngine on statement.

301 Redirect, One Rewrite Works And The Other Does Not

Sorry for the long title. I'm currently writing some 301 redirects and using mod rewrite (Apache) to handle them. Currently only one of the two 301s I have tried is working. Here is the code:
#301 REDIRECTS
RewriteEngine On
RewriteRule ^Fox-and-Frank-home\.html$ http://www.mydomain.co.uk/contact_us.php [R=301]
RewriteRule ^about\.html$ http://www.domain.com/about/ [R=301,L]
about.html properly redirects, but Fox-and-Frank-home.html does not. I have tried this with other names, other URLs, but it is not working. Any help will be greatly appreciated.
EDIT
I have gotten this to work on a completely bare .htaccess file. Do 301 redirects need to be at the very top before everything else?
You don't have a L (last) flag on your Fox-and-Frank rewrite, so other rules can potentially be processed, and definetly will be in a .htaccess (where you need to use the END flag).
Try changing it to:
RewriteRule ^Fox-and-Frank-home\.html$ http://www.mydomain.co.uk/contact-us.php [NC,R=301,L]
Where:
NC makes it a case insensitive check (so fox-and-frank-home would
redirect to)
R=301 is the 301 redirect (although you can just put R
and 301 would be assumed I always specify it personally)
L tells
mod_rewrite to stop processing further rules (but as its in a
.htaccess. you may need to use END instead - see http://httpd.apache.org/docs/current/rewrite/flags.html#flag_l).
Regards.

Resources