301 redirect with a directory - .htaccess

Following works fine
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
Now I need to do the same but with a directory, like
www.mydomain.com/oldname/something to www.mydomain.com/NEWNAME/something

Before the rule that redirects the domain, add these rules:
RewriteRule ^/?oldname/(.*)$ http://www.mydomain.com/newname/$1 [L,R=301]

Related

Redirect every page except frontpage

I want to redirect all pages to a new website with the same site structure, e.g. olddomain.com/123.html to newdomain.com/123.html, this works perfectly fine with:
RewriteEngine On
RewriteCond %{REQUEST_URI} (.*)
RewriteRule ^(.*)$ https://newdomain.com$1 [L,R=301]
But i want the Frontpage to be redirected to newdomain.com/landingpage, i tried the following and it isn't working.
RewriteEngine On
Redirect 301 / https://newdomain.com/landingpage
RewriteCond %{REQUEST_URI} (.*)
RewriteRule ^(.*)$ https://newdomain.com$1 [L,R=301]
Can someone help me with this?
You can use this :
RewriteEngine On
#redirect homepage to a specific location on new domain
RewriteRule ^/?$ https://newdomain.com/landingpage [L,R=301]
#redirect everything else from old domain to new
RewriteCond %{REQUEST_URI} (.*)
RewriteRule ^/?(.*)$ https://newdomain.com/$1 [L,R=301]

using .htaccess to add www prefix

I have a site hosted on godaddy. uses apache.
I used this code in .htaccess to add www prefix to the domain automatically
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/../$1 [R=301,L]
but instead of 'www.example.com' it goes to 'www.example.com/web'
I just want to convert 'example.com' to 'www.example.com'
If you just want to convert example.com to www.example.com then you just need to use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=302,NC]
You can also lay it out like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]
Make sure you clear your cache before testing this. You will notice I've just the flag R=302. This is a temporary redirect, use this while you're testing. If you're happy with the RewriteRule and everything is working, change these to R=301, which is a permanent redirect.
solved by using this
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*)$ http://www.example.com/$1 [R=301]
RedirectMatch 301 ^/web/$ http://www.example.com/

Htaccess url redirecting error

I have the following structure in my htaccess file. My issue is if URI matches with condition URL redirect according to rule and if URI doesn't match default rule should apply, please tell me how to fix.
First Rule: RedirectMatch 301 /mob/(.*) http://www.newdomain.com/price/$1
Default Rule: RedirectMatch 301 /(.*) http://www.newdomain.com/$1
when I tried this default rule overriding other rules.
Assuming both domains are on the same root folder and host:
RewriteCond %{HTTP_HOST} ^originaldomain\.com$
RewriteCond %{REQUEST_URI} ^/cms
RewriteRule ^(.*)$ https://differentdomain.com/$1 [L,R=302]
If they are not on the same root and folder:
RewriteCond %{REQUEST_URI} ^/cms
RewriteRule ^(.*)$ https://differentdomain.com/$1 [L,R=302]
Now the 2nd part if the url is not a act, url, system or post:
RewriteCond %{THE_REQUEST} !^[A-Z]{3,}\s/(act\?(.*)|url|system)$ [NC]
RewriteCond %{THE_REQUEST} !^POST [NC]
RewriteRule ^(.*)$ http://originaldomain.com/$1 [L,R=302]
Basically this should work, if it does after you test change to 302 to 301 if needed.

301 Redirects with dynamic URLs

I am trying to rewrite my .htaccess file to redirect old dynamic URLS to new ones.
I have tried these so far but they are not working as expected. The second rule seems to redirect to the first one.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.com$
RewriteRule ^(.*)$ "http\:\/\/www\.mydomain\.com\/$1" [R=301,L]
Redirect 301 /product.php?l=old-product-name1 http://www.mydomain.com/product.php?l=new-product-name1
Redirect 301 /product.php?l=old-product-name2 http://www.mydomain.com/product.php?l=new-product-name2
Can anyone help me redirect these properly?
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain.com$
RewriteRule ^(.*)$ "http\:\/\/www\.mydomain\.com\/$1" [R=301,L]
RewriteCond %{QUERY_STRING} =l=old-product-name1
RewriteRule ^product.php$ http://www.mydomain.com/product.php?l=new-product-name1 [R=301]

Non WWW to WWW redirection using htaccess File

I am using Codeigniter-.
My domain currently does not redirect to www version.
For example if I type mydomain.com then it stays mydomain.com. I want to redirect it to www.mydomain.com.
If someone types mydomain.com/controller/method then it should be www.mydomain.com/controller/method.
Another problem: I already tried other solutions but the problem is when it redirects to www version, it automatically adds "index.php" in the URL. But when I type www in the domain name then it works fine, no "index.php" in the URL. This problem occurs only during the redirection.
Here is my .htaccess file (I've removed the redirection code)
RewriteCond $1 !^(index\.php|system|rpc_relay.html|canvas.html|robots\.txt)
RewriteCond $1 !^(sitemap\.xml|export)
RewriteRule ^(.*)$ /index.php/$1 [L]
Any help would be greatly appreciated.
To redirect from http:// to http://www. , and also remove the route file (index.php) in the url, put these lines on your htaccess :
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond $1 !^(index\.php|images|css|js|styles|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
the domain is :
domain.com
folder with direct access :
images|css|js|styles
hope this help
I've used the following before:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
</IfModule>
To redirect domain.com to www.domain.com, you could use the following rewrite rule. Please replace domain.com with your own domain name.
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
I don't know why there are such complex RewriteRules answers, even though Gobhi has provided a nice generic solution (= whatever the domain name is, it works).
Here's my solution.
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
RewriteRule (.*)/index\.php$ $1/ [QSA]
</IfModule>

Resources