What's wrong with this Apache mod_rewrite rule? - apache

I'm attempting to write some redirects for an Apache site, but my mod_rewrite skills are rusty. What's wrong with this rule?
RewriteRule ^/en/(.*)$ /$1 [R=301,L]
I expect it to redirect http://example.com/en/whatevs.html to http://example.com/whatevs.html, but it does not seem to match.

RewriteEngine On
RewriteRule ^en/(.*)$ /$1 [R=301,L]
You were close. It's hard in rewrite to remember when to use the starting / and when not to. I put in the rewriteengine on in case it had slipped your mind to include that.
For example:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/en/.*$
RewriteRule ^en/(.*) /$1 [R=301,L]
Note which has the slash and which does not.
And there's some subtle differences also depending on if your rules are in httpd.conf files or in .htaccess files, but I forget the exact differences.

Related

Mod_rewrite - url rewriting isn't working

I've been trying to get the mod_rewrite in my .htaccess file to work for the past few hours but I can't seem to pull it off. The .htaccess files definitely work on my server because I've used them before.
I've tried to rewrite it with this generator and testing it with this tool as well as on my server.
URL
I need to turn
http://someurl.com/news-detail.html?id=10&news=this-is-an-article
into
http://someurl.com/news/10/this-is-an-article
or
http://someurl.com/10/news/this-is-an-article
Rewrite
I've tried:
RewriteEngine On
RewriteRule ^([^/]*)/news/([^/]*)$ /news-detail.html?id=$1&news=$2 [L]
and
RewriteEngine on
RewriteRule /(.*)/news/(.*) news-detail.html?id=$1&news=$2 [L]
and
RewriteEngine on
RewriteRule news/([0-9]+)/([a-zA-Z_]+)$ news-detail.html?id=$1&news=$2 [L]
and many others.
I hope someone can help me out here..
Kindly check/add RewriteBase in your .htaccess file.
Also check if htaccess is allowed to rewrite or not.
Syntax is below
RewriteEngine on
RewriteBase /flodername/
RewriteRule ^urlinbrowser/(.*)/(.*)$ filename.php?first_param=$1&secondparam=$2 [L]
Keep your rules like this in root .htaccess:
RewriteEngine On
RewriteBase /dev/hoig/
RewriteCond %{THE_REQUEST} /news-detail\.html\?id=([^\s&]+)&news=([^\s&]+) [NC]
RewriteRule ^ %1/news/%2? [R=302,L]
RewriteRule ^([^/]+)/news/([^/]+)/?$ news-detail.html?id=$1&news=$2 [L,NC,QSA]
This will support /10/news/this-is-an-article URI structure for pretty URLs.

Apache mod_rewrite .htaccess rules not matching - escaped characters

I'm trying to do some relatively simple .htaccess rules to 301 redirect some URLs. The rules are not matching. Here is what I have:
RewriteEngine On
RewriteBase /
# This works fine
RewriteCond %{QUERY_STRING} ^id=2$ [NC]
RewriteRule ^products\.php$ /? [R=301,NE,NC,L]
# This doesn't match at all
RewriteRule ^products/-Smart-Smoker-\'Storm\'-White-Manual-Electronic-Cigarette-\(510\)\.html http://www.smartsmoker.co.uk/products/-Smart-Smoker-Storm-White-Manual-Electronic-Cigarette-%28510%29.html [NC,R=301]
# Neither does this
RewriteRule ^products/Christmas-Cracker-%252d-FREE-Shipping\.html$ http://www.smartsmoker.co.uk/categories/Electronic-Cigarette-Kits/Breeze-Mini-Electronic-Cigarette/ [L,R=301]
Best bet when having rewrite drama is to break it down into the shortest functioning match then expand on it noting that conditions above might be affecting what you want to actually test. Having said that, I can't see any problems myself using the minimal example below:
RewriteEngine on
RewriteBase /
RewriteRule ^products/-Smart-Smoker-\'Storm\'-White-Manual-Electronic-Cigarette-\(510\)\.html$ /test.html [L]
Also the URLs you redirect to don't have to be URL encoded.

htaccess mod rewrite NOT

I have a small problem with url rewriting on apache.
I would like it that it ignores the admin/ folder from rewriting.
Options +FollowSymLinks
RewriteRule ^([^/]*)/([^/]*)\.html$ /index.php?cat=$1&name=$2 [L]
RewriteRule ^([^/]*)/$ /index.php?cat=$1 [L]
I have triend doing it myself but I can't figure it out.
Thanks.
You can use RewriteCond to put conditions on a RewriteRule. Unless all of the conditions match, the RewriteRule won't be applied. In your case, I'll assume your admin folder is located at http://yoursite.com/admin, so a rule like this should work:
RewriteCond %{REQUEST_URI} !^/admin/*
Put that before the RewriteRule that you want to prevent from being applied. The order of RewriteCond and RewriteRule directives is important, so be sure of where you're putting it.
I think this may be more of a ServerFault thing, but there's a really quick answer: if you put
RewriteRule ^admin/ - [L]
before your other rewriting rules, that should prevent any URL transformations from being applied to URLs starting with admin/.

Apache Redirect problem in .htaccess

I am having problems getring a simple redirect statement to take effect on my Godaddy account. I have the following statements in my .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.mydomain.net$ [NC]
RewriteRule ^(.*)$ http://mydomain.net/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^mydomain.net$ [NC]
RewriteRule ^/lists/$ / [R=301]
RewriteCond %{HTTP_HOST} ^mydomain.net$ [NC]
RewriteRule ^/blog/$ http://myotherdomain.net/ [R=301]
The 1st redirect ALWAYS work. The 2nd and 3rd ones however, NEVER work. I just get a 404 from the server. The Apache logs do not reveal any useful infomation - just a 404.
Any ideas, anybody?
Your help will be greatly appreciated.
Thanks
Per-directory Rewrites
When using the rewrite engine in .htaccess files the per-directory prefix (which always is the same for a specific directory) is automatically removed for the pattern matching and automatically added after the substitution has been done.
– http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule
So just leave the leading slash out of the pattern.
For simple redirects like that, better use the simple RedirectMatch directives:
RedirectMatch 301 ^/lists/$ http://mydomain.net/
RedirectMatch 301 ^/blog/$ http://myotherdomain.net/
If you insist on using rewriting make sure you add the L flag to your rules.
Apache mod_rewrite Flags says :
You will almost always want to use [R] in conjunction with [L] (that is, use [R,L]) because on its own, the [R] flag prepends http://thishost[:thisport] to the URI, but then passes this on to the next rule in the ruleset, which can often result in 'Invalid URI in request' warnings.
Simply remove the slashs at the beginning. It also might be useful to make the slashs at the end optional.
RewriteCond %{HTTP_HOST} ^mydomain.net$ [NC]
RewriteRule ^lists/{0,1}$ / [R=301]
RewriteCond %{HTTP_HOST} ^mydomain.net$ [NC]
RewriteRule ^blog/{0,1}$ http://myotherdomain.net/ [R=301]
Put the first one last. Once it encounters a redirect match, it runs it and ignores the rest.

Make apache automatically strip off the www.?

For various reasons, such as cookies, SEO, and to keep things simple, I would like to make apache automatically redirect any requests for http://www.foobar.com/anything to http://foobar.com/anything. The best I could come up with is a mod_rewrite-based monstrosity, is there some easy simple way to tell it "Redirect all requests for domain ABC to XYZ"?
PS: I found this somewhat related question, but it's for IIS and does the opposite of what I want. Also it's still complex.
It's as easy as:
<VirtualHost 10.0.0.1:80>
ServerName www.example.com
Redirect permanent / http://example.com/
</VirtualHost>
Adapt host names and IPs as needed :)
simpler and easier to copy from site to site:
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Pretty simple if you use mod_rewrite, as we all do ;)
This is part of the .htaccess from my live website:
RewriteEngine on
# Catches www.infinite-labs.net and redirects to the
# same page on infinite-labs.net to normalize things.
RewriteCond %{HTTP_HOST} ^www\.infinite-labs\.net$
RewriteRule ^(.*)$ http://infinite-labs.net/$1 [R=301,L]
Use an .htaccess file with some mod_rewrite rules:
RewriteEngine On
RewriteRule ^www.SERVERNAME(.*) http://SERVERNAME$1 [L,QSA]
I'm not sure I got the syntax right with the $1 there, but it's well documented. L sends a location: header to the browser, and QSA means Query String Append.
Since you mentioned using mod_rewrite, I'd suggest a simple rule in your .htaccess - doesn't seem monstrous to me :)
RewriteCond %{HTTP_HOST} ^www\.foobar\.com$ [NC]
RewriteRule ^(.*)$ http://foobar.com/$1 [L,R=301]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
That should do the trick.