redirect a url or domain with www - url

I have a simple question about url or domain redirect. I was looking through the previous questions on how to redirect a url or domain from http to http to force using ssl.
The weird part shows up when I try to redirect none www. to www. with the domain. I tried many ways, but none worked for me. does any one have any idea why is that?? and how to resolve this issue??
here is the code for I used at .htaccess:
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$
RewriteRule ^/?$ "https\:\/\/www\.mydomain\.com\/main" [R=301,L]
another weird thing is that whenever I typed the domain on the browser without www. there is an error message, but when I insert the www. in front of the domain, it redirects with https and wwww of course.
any idea?
Thanks

You rules will run into a loop. Do this instead:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Related

Rewrite URL with Query and directory in path

I have a URL of the form:
http://www.example.com/ads/story.asp?story_id=73
that I would like to replace with:
https://www.example.com/news/awards-2015/
I have tried several methods from other stackoverflow posts, including the following:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/ads/story.asp$
RewriteCond %{QUERY_STRING} ^story_id=73$
RewriteRule ^(.*)$ https://www.example.com/news/awards-2015/ [R=301,L]
... without success.
Can anyone suggest what I might be doing wrong? Key differences from other examples appear to be 1) rewriting from an http URL to an https URL and 2) the additional directory "news" in the path of the URL to be rewritten.
Any help would be appreciated!
Got it working.
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)story_id=73(&|$) [NC]
RewriteRule ads/story_detail.asp(.*)$ https://www.example.com/news/awards-2015/? [R=301,L]

bluehost shared ssl redirect with htaccess

I am wanting to add a redirect to my bluehost shared ssl page:
https://secure.bluehost.com/~user/page_to_redirect
to here:
http://mydomain.com/folder/new_page
I need this setup, as I am using a website's API, which requires that the callback url is ssl. And i need this redirected to my ruby on rails app.
i have configured my .htacccess (root) like so:
Options -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.mydomain.\.com$
RewriteRule ^/?$ "http\:\/\/mydomain\.com\/" [R=301,L]
Redirect /page_to_redirect http://mydomain.com/folder/new_page
the redirect works for the non ssl page:
mydomain.com/page_to_redirect
but not the ssl page:
https://secure.bluehost.com/~user/page_to_redirect
I just get a 404 error.
does anyone have any experience with this?
Bluehost support was not very helpful at all.
this is what worked:
Options -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.mydomain.\.com$
RewriteRule ^/?$ "http\:\/\/mydomain\.com\/" [R=301,L]
Redirect 301 /~mydomain/page_to_redirect http://mydomain.com/folder/new_page
This is what worked:
Options -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.mydomain.\.com$
RewriteRule ^/?$ "http\:\/\/mydomain\.com\/" [R=301,L]
Redirect 301 /~mydomain/page_to_redirect http://mydomain.com/folder/new_page

.htaccess rewrite frustrations

I made my website in PHP originally and used parameters for page/product identification.
ex.) http://mysite.com/?pid=my_products&sid=golf_clubs&GTIN=111111111111
I've now moved over to using RoR and I'd like to set up redirects to clean up the urls
http://mysite.com/my_products/golf_clubs/111111111111
I've looked at tutorials and arrived at this:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ ?pid=$1&sid=$2&GTIN=$3 [NC]
I've tested a url and it doesn't redirect at all. I've tried putting this in the .htaccess file to debug and chmodded the log folder to 775 but I get a 500 error:
RewriteLog "/home/myaccount/rails_apps/myapp/log/rewrite.log"
RewriteLogLevel 3
As far as rewrite statements in .htaccess goes, this is what I have:
RewriteEngine On
RewriteLog "/home/myaccount/rails_apps/myapp/log/rewrite.log"
RewriteLogLevel 3
RewriteBase /
RewriteCond %{REQUEST_URI} ^/downloads.*
RewriteRule .* - [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ pid=$1&sid=$2&GTIN=$3 [NC]
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
ErrorDocument 500 "<h2>Application error</h2>Rails application failed to start properly"
* I am on a shared hosting plan with Bluehost (good gawd I wish they'd update to Rails 3 soon)
If I understand you correctly, you have your rule backwards. You want to catch your old URLs and rewrite them to the new format, correct? In that case, you want this:
RewriteRule ^\?pid=(\w+)\&sid=(\w+)\&GTIN=(\w+) /$1/$2/$3 [NC,R=301,L]
Adding a 301 status will tell the user agent (including well-behaved search bots) that your canonical URLs have permanently changed.

.htaccess non-www to www URL Redirect Erases any URL path beyond base url

When visitors go, to say,:
http://website.com/dir/dir2/file.html
My re-write clears the path beyond the base url and redirects them to the home page with www. in the front:
http://www.website.com/
How can I prevent the stripping from occurring and sending visitors to the homepage instead of their requested url? FYI, the URLs in question DO exist.
Thanx!
Include the file/path info on the end of the rewrite like this:
RewriteCond %{HTTP_HOST} !^www\.website\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.website.com/$1 [L,R]
FYI, this is for a Magento site.
In .htaccess, after
#RewriteBase /magento/
After it I placed on their own lines:
RewriteCond %{HTTP_HOST} !^www\.website\.com$ [NC]
RewriteRule ^(.*)$ http://www.website.com/$1 [L,R=301]
And it works. Dad with kids also had it right, just don't know what the second line was for.
Most people need RewriteBase set, so try:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^(?:www\.your-live-domain\.com|staging\.domain\.com)$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule .* http://www.your-live-domain.com/$0 [L,R=301]
Use 301 to make it permanent

Rewriting rails url from port 3000?

i have an applicaiton that runs at actionengineers.com:3000. How do i reqwite the application to a subdomain or directory (actionengineers.com/myapp) without disturbing the main site of course. i tried to use .htaccess file but i allways get eror 404. here is the code in the .htaccess file.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.deep.actionengeneers.com$
RewriteRule ^/?$ "http\:\/\/127\.0\.0\.1\:3000%{REQUEST_URI}" [P,QSA,L]
Try this rule:
RewriteCond %{HTTP_HOST} =www.deep.actionengeneers.com
RewriteRule ^ http://127.0.0.1:3000%{REQUEST_URI} [P,L]
Now all requests to www.deep.actionengeneers.com are getting passed through to 127.0.0.1:3000 via proxy.

Resources