How do I use .htaccess to force www. while using Zend Framework - zend-framework

I want to force a www. prefix on my website by using a .htaccess 301 redirect. I am currently trying:
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
Which normally works, but I am using Zend Framework which causes all requests to be redirected back to http://www.mysite.com/index.php regardless of the initial request.
For example...
http://mysite.com/blog,
http://mysite.com/contact,
http://mysite.com/blog/this-is-my-article,
Will all be redirected to http://www.mysite.com/index.php
However, if I initially request a specific file, such as...
http://mysite.com/some-file.htm
The redirect works properly, redirecting to http://www.mysite.com/some-file.htm

In first time, don't forget to enable the rewriting ("RewriteEngine on").
The last line is important if you use Zend Framework.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
RewriteRule !\.(pdf|php|js|ico|txt|gif|jpg|png|css|rss|zip|tar\.gz)$ index.php
Now the url...
http://mysite.com/some-file.htm
... redirect to http://www.mysite.com/some-file.htm but use the index.php

Don't forget to ignore sub-domains when re-directing to www.
http://www.theblogaholic.com/2011/01/16/force-www-using-htaccess-except-for-subdomains/

Related

Redirecting full URL including directories

I'm migrating to a new website.
I want to be able to redirect everything from the old website to the new website in the following structure.
Example 1:
http://dev.website-old.com/abc/123
to
http://dev.website-new.com/abc/123
Example 2:
http://dev.website-old.com/abc/123/xyz
to
http://dev.website-new.com/abc/123/xyz
How do I accomplish this?
This rewrite redirect all requests (1:1) from dev.website-old.com to dev.website-new.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?dev\.website-old\.com [NC]
RewriteRule (.*) http://dev.website-new.com/$1 [R=301,L]

How to make a redirect in .htaccess for pages from different subdomains

I'm not very advanced at programming and I need to make a 301 redirection from one specific subdomain page to another. The tricky part, at least for me, is that the pages are on two different subdomains. So far I've only managed to 301 ALL pages to the target or crashing the whole site.
I've used many, but are currently at:
RewriteCond %{HTTP_HOST} !^spiele.deutschedownloads.de/?DriverScanner/Diverse//download/4770$ [NC]
RewriteRule ^(.*)$ http://programme.deutschedownloads.de/?DriverScanner/Drivers/Werkzeuge-Diverse/download/4413 [L,R=301]
What I want to do is simply to 301 redirect
spiele.deutschedownloads.de/?DriverScanner/Diverse//download/4770
To
http://programme.deutschedownloads.de/?DriverScanner/Drivers/Werkzeuge-Diverse/download/4413
Any help is most appreciated!
This should work:
RewriteCond %{HTTP_HOST} ^spiele\.deutschedownloads\.de$ [NC]
RewriteCond %{QUERY_STRING} ^DriverScanner/Diverse//?download/4770/?$ [NC]
RewriteRule ^ http://programme.deutschedownloads.de/?DriverScanner/Drivers/Werkzeuge-Diverse/download/4413 [R=301,L]
First condition matches the domain in case they are both on the same root folder.
Second condition matches the query string.
If both conditions are OK it redirects.

Redirecting error using .htaccess

rewriting the url RewriteRule ^about/ /about.php [L,R=301] but the page is accessed via /about.php directly also. Please help to how to redirect .php file
Replace your code with this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+about\.php\s [NC]
RewriteRule ^ /about/ [R=302,L]
RewriteRule ^about/?$ /about.php [L,NC]
You can use this free .htaccess Redirect Generator:
http://htaccessredirect.net/
with this tool you can easily create your redirection code for .htaccess.
You can use rule to hidden redirect:
#use [L] to hidden [R] to shown redirect
RewriteRule ^(about|faq|home)/?$ $1.php [L]
#to prevent access from about.php simply do this:
RewriteRule (.+)\.php$ $1/ [R=301]
#you need to know 301 redirect will be cached with browser for long time
#so if you change rule - will be no effect for some visitors

redirecting just a single non-www homepage to www version, without redirecting any other pages

I need to redirect the non-www version of just a single page, the homepage, to www.
I want to leave the rest of the site alone. Due to a need to keep a specific page non www.
So if i try a rewrite rule of
/index.html http://www.example.com/
I get a infinite redirect type error.
There's probably a way to achieve what you want, although I must say that this has a bit of a "smell" to it. It sounds like you are trying to treat a single symptom rather than fix a problem (which, admittedly might be beyond your control). Is it possible that your organization needs to setup a domain pointer so that www.example.com and examples.com are the exact same thing?
So you want
http://example.com/ to go to http://www.example.com/,
but you want
http://example.com/index.html,
http://example.com/otherstuff, and
http://example.com/subdir/path/file.php
to stay the same?
For Apache:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^/$ http://www.example.com/ [R=301,L]
If you want any non-www subdomain on the site to get redirected to the www version:
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^/$ http://www.%{HTTP_HOST}/ [R=301,L]
If you want to redirect everything except a specific page:
RewriteCond %{REQUEST_URI} !^/subdir/path/file.php$
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Redirect URL which contains "option=com_virtuemart"

I want to 301 redirect some URLs which contain "option=com_virtuemart".
I tried with this code:
RewriteCond %{QUERY_STRING} ^option=com_virtuemart
RewriteRule ^(.*)$ http://www.newsite.com/$1 [L,R=301]
But it doesn't work.
The code is working, but the server is disabled htaccess controll..