htaccess Redirect subdirectory and its contents - php

Here is my initial redirect rule:
RewriteCond %{HTTP_HOST} !^sub1\.myinitialdomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^sub2\.myinitialdomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [L,R=301]
That basically redirects the entire website, except for sub1 and sub2 to the new domain.
However, I've got a wordpress install at /wordpress and I've got the following rule in place to redirect all of the articles:
RewriteCond %{REQUEST_URI} wordpress
RewriteRule ^(.*)$ https://www.mydomain.com/blog/$1 [R,L]
But even though the articles are being redirected correctly, the main /wordpress directory is not being redirected correctly. It's being redirected to https://www.mydomain.com/blog/wordpress
I need to be able to combine the rules, but make sure that /worpress and any articles within that are being redirected to https://www.mydomain.com/blog/

You can use this rule to redirect everything from within /wordpress/.htaccess file:
RewriteEngine On
RewriteRule ^(.*)$ https://www.mydomain.com/blog/$1 [NC,L,NE,R=302]
RewriteCond %{HTTP_HOST} !^(sub1|sub2)\.myinitialdomain\.com$ [NC]
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [L,NE,R=302]
Make sure this is first rule in /wordpress/.htaccess file.

Related

.htaccess - How to redirect file requests in a subdomain

I have a website that has custom subdomains for users, e.g. creator.snaprate.me.
This works great, but if I manually type in creator.snaprate.me/login the login page (snaprate.me/login) appears.
I'd like to have a redirect to snaprate.me/anypage for any subdomain, but don't know how.
I have the following .htaccess, and this has been working great except for this issue:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^((?!www\.)[^.]+).snaprate\.me$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^((?!www\.)[^.]+).snaprate\.me$
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
What do I need to add in order to redirect traffic from subdomain.snaprate.me/anypage to snaprate.me/anypage? I'd really like an answer to this; I have been searching for days and still I have no idea what to do -- htaccess is my Achille's heel!

Redirect all subfolder requests to a specific folder

I am trying to redirect all the requests to a specific folder inside my domain.
For example:
www.example.com/whatever needs to be redirected to www.example.com/specifiedfolder by preserving the requested address www.example.com/whatever.
I tried this:
RewriteCond %{HTTP_HOST} ^example\.com\xxx
RewriteRule ^(.*)$ http://example.com/com1/$1 [R=301]
Try this method which will redirect as you expect
RewriteCond %{REQUEST_URI} !^/specifiedfolder/
RewriteCond %{REQUEST_URI} !^/admin/
RewriteRule ^(.*)$ /specifiedfolder/$1 [L]
You can use this rule to rewrite (not redirect) from root to another folder:
RewriteEngine On
RewriteRule !^specifiedfolder/ /specifiedfolder%{REQUEST_URI} [L,NC]
Make sure this rule is placed in site root .htaccess not in any sub-directory.

Redirect sub domain to sub directory

I want to redirect the the subdomain to subdirectory but not working. Here is my efforts.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^name\.site_url
RewriteRule ^(.*)$ http://site_url/name/$1 [L,R=301]
Try this:
RewriteCond %{HTTP_HOST} ^sub\.
RewriteRule ^(.*)$ http://your_domain/sub/$1 [R,L]
Instead sub in RewriteCond and RewriteRule you can place whatever you want
Btw, RewriteCond %{HTTP_HOST} ^sub\.my\.domain$ also works for me. So, check your site url. Or give more info (at least, what happened in apache error.log).

htaccess permanent redirect to home page

I want to write a rule in htaccess that always redirects http://example.com/index.aspx or http://www.example.com/index.aspx to http://example.com.
This is what I have tried so far:
RewriteCond %{REQUEST_URI} ^/index.aspx [NC]
RewriteRule ^(.*)$ http://%1 [R=301,L]
but it's not working properly.
Thanks
You can use just one rule:
RewriteRule ^index\.aspx$ http://%{HTTP_HOST} [NC,R=301,L]

Virtual subdomain using htacess, Pages rewrite rules

I have make virtual subdomain in my code.like below
RewriteCond %{HTTP_HOST} ^(.*)\.mysitename\.com
RewriteRule ^(.*)$ agent.php?asitename=%1 [L,NC,QSA]
it works fine, but it did not work for pages like
RewriteCond %{HTTP_HOST} ^(.*)\.mysitename\.com
RewriteRule ^(.*)/ag_buy.html ag_buy.php?sitename=%1&page=buy [L,NC,QSA]
it redirect all pages top agent.php, but it should only redirect home page to agent.php, for other pages it should work like ag_buy.html to ag_buy.php
and so on.........
please guide me on htaccess how can i make this possible.
The ^(.*)$ matches everything, if you only want the home page, then change it to ^$:
RewriteCond %{HTTP_HOST} ^(.*)\.mysitename\.com
RewriteRule ^$ agent.php?asitename=%1 [L,NC,QSA]
Try to replace the first rule with following code:
RewriteCond %{HTTP_HOST} ^(.).mysitename.com
RewriteCond %{REQUEST_URI} =/
RewriteRule ^(.)$ agent.php?asitename=%1 [L,NC,QSA]

Categories