htaccess subdomain redirect to folder - .htaccess

I am trying to get my subdomain to show the article path as well but it won't work.
if I go to blog.domain.com, it redirects to https://www.domain.com/blog/
I want https://www.domain.com/blog/ to stay blog.domain.com
and if I go to https://www.domain.com/blog/article/1 to show blog.domain.com/article/1/
With godaddy's cpanel, I added the subdomain forwarding (without masking - as it was always showing blog.domain.com and I want to see the path so I removed the masking)
I tried:
RewriteCond %{HTTP_HOST} ^(www\.)domain\.com$
RewriteRule ^blog/(.*)$ http://blog.domain.com/$1 [L,R=301,QSA]
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$
RewriteCond %{REQUEST_URI} !blog/
RewriteRule ^(.*)$ blog/$1 [L,QSA]
This did not seem to do anything..
EDIT
I placed the htaccess only in the root folder.
And I also tried
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule ^blog(.*)$ http://blog.domain.com/$1 [L,R=301,QSA]
when i go to blog.domain.com it redirects to www.domain.com/blog/ and the page does not load

I have it set to: when going to blog.domain.com -> redirect to www.domain.com/blog
This is why your browser's location bar is changing. When you redirect, the server is responding to the browser by saying "What you're looking for is not at the URL you just went to, it is at (in the case of 301, permanently) this NEW URL", so the browser loads the new URL, sending an entirely new and separate request to the new URL. That causes the location bar to change.
Turn off that redirect.
Then you should be able to leave the rule that you have in the root folder for your main domain:
RewriteCond %{HTTP_HOST} ^(www\.)domain\.com$
RewriteRule ^blog/(.*)$ http://blog.domain.com/$1 [L,R=301,QSA]
You don't need to mess with that other rule since your *document root for the blog.domain.com is already /blog/.

Related

htaccess redirect url not working /?index.html to /

I have a client project on search engine marketing with a website https://www.iehsacademy.com/,
The site is 13 years old. before the client was using the URL structure like with HTML like this: https://www.example.com/?index.html (Home Page),
https://www.example.com/?cndc.html,
But last year they completely remake the website URL structure and make it to https://www.example.com/ (Home Page), https://www.exsample.com/about. https://www.example.com/contact.
Now, The old URLs and the new URLs are both working. The client wants and also for SEO purposes we want to block the old URL or redirect them to the new URL. I tried many rewrite conditions on htaccess
RewriteRule ^([a-z]+).html https://www.iehsacademy.com/ [QSA,L]
This one also
RewriteCond %{HTTP_HOST} ^www.iehsacademy.com/?cndc.html$
RewriteRule (.*)$ http://www.iehsacademy.com/$1 [R=301,L]
Not Working anything. The client's developer is also confused. Can anyone help with this or where i am doing wrong?
NB: I took permission from the site owner to share the client's URLs and problems.
You can use the following redirection rule :
RewriteEngine On
#redirect /?index.html to /
RewriteCond %{HTTP_HOST} ^www.iehsacademy.com$ [NC]
RewriteCond %{QUERY_STRING} ^index\.html$
RewriteRule ^$ https://example.com/? [L,R=301]
#redirect /?foobar to /foobar
RewriteCond %{HTTP_HOST} ^www.iehsacademy.com$ [NC]
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^$ https://example.com/%1? [L,R=301]
Make sure to clear your browser caches or use a different browser to test the redirection. And do not forget to replace the destination domain example.com with your domain name.

301 Redirect all pages to root, not index.html, and also redirect all non-www to www

I've been looking for a precise answer to this question for awhile but couldn't find it.
I've launched a one page website in place of an old website with many pages. Now, I want to redirect everything to www.domain.com. The page uses index.html as the homepage, but I don't want to redirect to that, I just want to redirect to the www.domain.com root.
I tried using:
RewriteRule ^.+$ / [R=302,NC,L]
But that just broke my stylesheet and didn't redirect anything. Other solutions I've seen have redirected to the index.html but I want to redirect to the / root domain.
Also, I want to be sure to redirect all non-www pages to www pages too. Can someone please help me out?
Much appreciated
You have most likely something like this in your httpd.conf:
<IfModule dir_module>
DirectoryIndex index.php index.php3 index.html index.htm
</IfModule>
If you request a folder (and http://example.com/ is a request to a folder), it will try index.php first, then index.php3, etc, etc, and the first one that exists it will shown. You'll have to delete that from your httpd.conf if you want every request to end up as http://example.com. It'll show a directory view of your www-root folder unless that has been disabled.
Try:
# Any direct request for html/php pages
RewriteCond %{THE_REQUEST} /[^\?\ ]+\.(html?|php.) [NC]
RewriteRule ^ / [L,R=301]
That won't affect images, or style sheets, etc. Note that this matches against the %{THE_REQUEST} variable because internally the %{REQUEST_URI} gets converted to /index.html so you can't match against that.
If you want non-existent requests (which would normally result in a 404) to be redirected as well:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ / [L,R=301]
To force a "www" use:
RewiteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

Taking a site down temporarily with Codeigniter (301 possible with routes.php?)

I'm using Codeigniter with the standard .htaccess rewrite rules so that no /index.php/ is visible within the urls.
Now I recently needed to take the site down temporarily and therefore wanted to redirect everyone to a 'down' page. The following worked:
$route['default_controller'] = "down";
$route['(:any)'] = "down";
But I'm aware that in this case, a 301 is really appropriate.
How and where should I set that? I don't see a way to specify it in the routes.php and was confused about how to do it within the .htaccess because of the existing rules....
RewriteBase /
RewriteCond %{REQUEST_URI} ^_system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^myapp.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond $1 !^(index\.php|resources|files|robots\.txt)
RewriteRule ^(.*)$ index.php/$1
You don't want a 301 redirect. 301 means "Permanent", and this should only be a temporary redirect. And you can do this with htaccess if you add this above all your other rules:
RewriteRule ^ /down.html [L,R=302]
As long as it's above any of the other rules, then any request will get redirected to /down.html. If you don't want to externally redirect the browser (e.g. have the /down.html URL show up in the URL address bar), then remove the ,R=302 bit from the square brackets and the URL address bar would stay unchanged while the content served is from down.html.

.htaccess combination redirect/ hide subfolder from url AND force insertion of "www" to url

I pretty much have this working. My problem is, as soon as I click on any link inside of the site, the subfolder name is added to the url. The modifications I have made to the htaccess file are all in the root directory, the one in the site directory is the standard joomla htaccess file. I am not sure if that is messing it up. Here is an example of the code.
To rephrase, I type the url into the browser "domain.com" The htaccess does its' job and changes that to "www.domain.com" automatically. I know that it is routing to the subfolder, because I can see the actual site. Then I click on the "Home" button as an example. The URL then shown in the address bar is "www.domain.com/site"
##### Redirect non-www to www -- BEGIN
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R,L]
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^/?(.*)$ http://www.domain.com/$1 [R=301,L]
##### Redirect non-www to www -- END
##### Subfolder redirect --BEGIN
RewriteRule ^(.*)$ /site/$1 [L]
RewriteRule ^(/)?$ site/index.php [L]
##### Subfolder redirect --END
I would be happy to post the entire htaccess file if needed, but I can tell you that through changing these two entries into it, I have gotten the site to where it is now. I have quite a few "security" measures in the file.
EDIT:
I should have put this in from the beginning, this is my attempt to redirect away from direct calls to the index.php. I just noticed that it may be fighting with my other redirect.
##### Redirect index.php to / -- BEGIN
RewriteCond %{THE_REQUEST} !^POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteCond %{SERVER_PORT}>s ^(443>(s)|[0-9]+>s)$
RewriteRule ^index\.php$ http%2://www.domain.com/ [R,L]
##### Redirect index.php to / -- END
EDIT 2:I have been constantly dumping my web cache from my browser for testing. My site seems to be running very slow right upon first connection and there is almost nothing on it as far as content. Have I added something that is requesting too much?

How to redirects all request to www directory via .htaccess?

I'm trying to create a .htaccess that would for a subdomain "test" redirects all requests this way:
http://test.mydomain.com/something/something2 (1)
to
http://test.mydomain.com/www/something/something2 (2)
so that in browser's address bar is still the address (1)?
I have Apache 2.0.
I'm trying to write it for two hours and I can't still find the right way.
Thanks!
You'll want something like this:
RewriteEngine On
# Only redirect if we're on the subdomain and haven't redirected
# (internally) to /www/ yet
RewriteCond %{HTTP_HOST} ^test
RewriteCond %{REQUEST_URI} !^/www/
RewriteRule ^.*$ /www/$0 [L]
# Let's also prevent them from being able to go to our /www path manually
RewriteCond %{THE_REQUEST} ^(POST|GET)\s/www
RewriteRule ^www/?(.*)$ http://test.mydomain.com/$1 [R,L]

Resources