htaccess redirect specific subfolder - .htaccess

I'm would like to setup a htaccess HTTP 301 redirect from /abc/* to http://newdomain.com/xyz/*.
I'm trying with this rewrite rule, but it is not working as expected:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/abc/(.*)?$ [NC]
RewriteRule ^ http://newdomain.com/xyz%{REQUEST_URI} [R=301,L]
</IfModule>
It redirect /abc/test to http://newdomain.com/xyz/abc/test. How can I change the rewrite rule to remove the abc part from the request uri?

Change this line :
RewriteRule ^ http://newdomain.com/xyz%{REQUEST_URI} [R=301,L]
to :
RewriteRule ^ http://newdomain.com/xyz/%1 [R=301,L]
Bacuse the %{REQUEST_URI} in the first line including /abc/ so you should seperate it from URI .
your all rules should look like this :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/abc/(.*)?$ [NC]
RewriteRule ^ http://newdomain.com/xyz/%1 [R=301,L]
</IfModule>
So , %1 represent this (.*)? after /abc/ in RewriteCond
You could also summerize this rule like the folwoing :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^abc/(.*)$ http://newdomain.com/xyz/$1 [R=301,L]
</IfModule>
Note: clear browser cache the test

Related

Safely move my blog?

My blog is currently located at:
example.com/blog
with posts being something like:
example.com/blog/my-post
I want to edit my htaccess file so anyone that accesses
example.com/blog/my-post
would end up at:
example.com/my-post
How can I do this with an htaccess file?
UPDATE
Right now I have:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# www.
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
</IfModule>
That will forward any non-www traffic to www. However, I need to modify this to support /blog as well. I don't think my syntax is right:
RewriteCond %{HTTP_HOST} ^example\.com/blog$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
?
You cannot match /blog using HOST_NAME variable.
Have your htaccess like this:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# www.
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301,NE]
RewriteRule ^blog/(.*)$ /$1 [L,NC,R=301]
</IfModule>
This assumes there is no .htaccess in /blog/ directory. However if it is there for some reason then use this rule as first rule in /blog/.htaccess:
RewriteEngine On
RewriteRule ^(.*)$ /$1 [L,R=301]

.htaccess make https://www.mysite.com go to https://mysite.com

I'd like my htaccess file to make any url gone to with a www to be removed...
For example, if I was to go to:
https://www.mysite.com
I'd need to be redirected to:
https://mysite.com
The site is SSL encrypted, so all URL's should have https://
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^https://www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
</IfModule>
You can use this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(example\.com)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301,NE]

Rewrite Role does not apply

I have php website and I have created .htaccess file. I have two rules. One for force www and other for permalink.
I want to change following Url
http://www.yatha.tv/play.php?vid=1437&id=1
to
http://www.yatha.tv/1437/1.html
But this rule does not change
RewriteRule ^([^/]*)/([^/]*)\.html$ /play.php?vid=$1&id=$2 [L]
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^yatha\.tv$
RewriteRule ^(.*)$ http://www.yatha.tv/$1 [R=301,L]
RewriteRule ^([^/]*)/([^/]*)\.html$ /play.php?vid=$1&id=$2 [L]
</IfModule>

subdomain rewrite rule lithium framework

I plan to have this as a URL:
https://SubDomain.domain.com/<locale>/Controller/action/param1/param2/?query=param3
in Lithium Framework (li3.me)
What is the purpose of the htaccess files in Lithium?
It should redirect me through .htaccess as:
https://domain.com/<locale>/Conttroller/action/param1/param2/?query=param3&subdomain=SubDomain
In Lithium framework there are 3 .htaccess files:
1. /.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
2. /app/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
3. /app/webroot/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
# Uncomment the line below, to enable HTTP authentication running PHP as a CGI.
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !favicon.ico$
RewriteRule ^ index.php [QSA,L]
</IfModule>
I tried to edit the root /.htaccess file to get the subdomain name as a parameter without any success.
Pass subdomain as parameter
Dynamic subdomain rewrite htaccess
.htaccess Wildcard Subdomains
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} !mydomain\.com$ [NC]
RewriteRule ^(.*)$ app/webroot/$1?subdomain=%1 [L]
Do I have to rewrite rules in all 3 .htaccess files? If so, what are the changes needed?
Thanks!

Codeigniter url rewrite like subdomain url with .htaccess

Currently my Url is: http://www.domain.co.uk/index.php/city/details/city-name
I would like to change it to:
http://www.city-name.domain.co.uk/index.php/city/details/city-name
or:
http://www.city-name.domain.co.uk/city/details/city-name
Put the .htaccess file into the http ://www.domain.co.uk/ document root
To http ://www.city-name.domain.co.uk/index.php/city/details/city-name
RewriteRule ^(.*)/([^/]+)$ http://www.$2.domain.co.uk/$1/$2 [R=301,L]
To http ://www.city-name.domain.co.uk/index.php/city/details/city-name
RewriteRule ^index.php/(.*)/([^/]+)$ http://www.$2.domain.co.uk/$1/$2 [R=301,L]
If the server is the same, set above RewriteRule this line to prevent redirection loop
RewriteCond %{HTTP_HOST} !^www\.(.*).domain\.co\.uk [NC]
File content example
<ifModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.(.*).domain\.co\.uk [NC]
RewriteRule ^index.php/(.*)/([^/]+)$ http://www.$2.domain.co.uk/$1/$2 [R=301,L]
</IfModule>
To exclude domain.co.uk (whitout www)
<ifModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.(.*).domain\.co\.uk [NC]
RewriteCond %{HTTP_HOST} !^domain\.co\.uk [NC]
RewriteRule ^index.php/(.*)/([^/]+)$ http://www.$2.domain.co.uk/$1/$2 [R=301,L]
</IfModule>

Resources