URL rewrite blog.domain.com to www.domain.com/blog - .htaccess

I thought this was pretty simple, but it doesn't seem to be working for me. I put in my httpd.conf:
RewriteCond %{HTTP_HOST} ^blog\.domain\.com [NC]
RewriteRule ^(.+)$ http://www.domain.com/blog/index.php [L,R=301]
I looked at two other questions here, one recommended ProxyPass, which didn't seem to work either.
My other thought was a VirtualHost for blog.domain.com. What's the best way to do this?

In my .htaccess I use it like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} (^|.)blog\.domain\.com [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ http://www.domain.com/blog/$1 [L]
RewriteCond %{HTTP_HOST} (^|.)blog\.domain\.com [NC]
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ https://www.domain.com/blog/$1 [L]
Note that I use (^|.) for people who would write www.blog.domain.com!
Dont forget to handle https also.

Related

.htaccess Shorten URL with HTTPS

My apologies from the start as this has probably been answered in some way, and believe me I've tried all the "solutions" however I'm not good at .htaccess, and in a need of a quick help if I may ask.
What is bugging me that I managed to finally put the HTTPS www force on my domain to work, however I've ran into an issue where I do not want my url to be fully shown of my subdirectories,
For instance www.example.com/pages/link.html,
I want to be shown as www.example.com/link.html
Among other directories like .../img/, or .../sub_admin/
Current Code
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]
As well what I recently tried to add:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^pages/
RewriteRule ^(.*)$ pages/$1 [L]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]
You'll probably have tried this already. Add condition as required.
RewriteRule ^pages/link.html?$ http://example.com/link.html [R=301,NC,L]

https redirect with htaccess file

I wanted all my domains (http-www, http-non-www and also https-non-www) be redirected to HTTPS-WWW-example-com
My .htaccess code seems to be working, but I really don't know how to check it. I am very doubtful if it works OK because google WMT indexes a lot of pages separately and that's for sure that something is wrong. I really want to avoid too much redirects which it seems to be the case
Here is my code:
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Please guys if this code happens not to be good and you send me another one please write whole domain in it because I'm beginner and might screw something up. I appreciate your time.
RewriteEngine On
RewriteCond % { SERVER_PORT } 80
RewriteRule ^ (. * )$ https://yourdomain.com/$1 [R=301,L]
This should work for yah!
Try this code :
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
I would suggest instead relying on .htaccess, you should change domain's vhost config and redirect domains accordingly.

Redirecting from https to http with the exception of some URI's

So the problem I am currently having is that our entire website is indexed using https. we want to redirect to http using .htaccess. the problem is that we need a couple of URIs to still use https and I am not sure how to write the exception for URIs
I know the below example would work if our site functioned like this www.example.com/account.php but our site urls are like www.example.com/index.php?l=account
# Turn SSL on for account
RewriteCond %{HTTPS} off
RewriteCond %{SCRIPT_FILENAME} \/account\.php [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
how can i fix this, any guidance would be appreciated.
thanks
EDIT!
So this code below I have working but I would like to make one more exception that I cant seem to get to work, I also want the root (index.php) to only use http.
I tried using...
RewriteCond %{REQUEST_URI} !^/index.php
but this did not work
# invoke rewrite engine
RewriteEngine On
RewriteBase /
RewriteCond %{https} off
RewriteCond %{QUERY_STRING} !^l=product_detail
RewriteCond %{QUERY_STRING} !^l=product_list
RewriteCond %{QUERY_STRING} !^l=page_view
RewriteRule ^(.*)$ https://%{HTTP_HOST}/development/$1 [R=301,L]
thanks
Try
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/account.php
RewriteCond %{QUERY_STRING} !^l=account
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/account.php [OR]
RewriteCond %{QUERY_STRING} ^l=account
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

How would I use mod rewrite to send visitors to www?

This should be a very simple question to answer.
How would I setup a .htaccess file to send visitors from mydomain.com to www.mydomain.com?
I just had some Ajax headaches caused by the same-source rule, and this should be an easy way around it.
Don't do that. Doing reverse makes sense. See http://no-www.org/
But to answer:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^host.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.host.com$
RewriteRule ^/?$ "http\:\/\/www\.host\.com\/" [R=301,L]

.htaccess Wildcard Subdomains

I'm trying to implement a solution using .htaccess and wildcard subdomains so that
http://subdomain.example.com is mapped to http://example.com/index.php/accounts/subdomain/. My rules look something like:
RewriteCond %{HTTP_HOST} !www.example.com [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).example.com [NC]
RewriteRule ^(.*/) /index.php [PT,L]
Which works, but disregards everything else. When I try appending anything to the rule e.g:
RewriteRule ^(.*/) /index.php/hello [PT,L]
I get a 500 internal server error. How do I get this working?
You probably need to exclude the index.php from your rule:
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.com$ [NC]
RewriteRule !^index\.php($|/) index.php/accounts/%2%{REQUEST_URI} [PT,L]
This is an adaptation of the code I use to redirect subdomains on my own site. I make no claims to it being best practice but it works;
RewriteCond %{HTTP_HOST} ^(.*)\.com$ [NC]
RewriteCond %1 !^(www)\.example$ [NC]
RewriteRule ^.*$ http://www.example.com/index.php/accounts/%1/ [R=301,L]
Try changing your RewriteRule to
RewriteRule ^/(.*)$ /index.php/accounts/%1/$1 [PT]
That will rewrite the URL to one that includes the subdomain and the original request URI.
EDIT: maybe it needs to be
RewriteRule ^(.*)$ /index.php/accounts/%1/$1 [PT]
as mentioned in the comments.

Resources