.htaccess rule for removing www from url - php

Whenever someone hits
http://www.example/consumer-complaints
it should become
http://example.com/consumer-complaints
The copy&paste .htaccess rules do not seem to work for me.
It removes www when someone hits
> http://www.example/consumer-complaints
but it redirects to only http://example.com
instead of
http://example.com/consumer-complaints
Can someone write an .htaccess rule for me
Thank you

The .htaccess Code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
Conversely, if you'd prefer to use "www" in all of your URLs, you can code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

Related

Subdomain URL Redirect dynamically. .htaccess

I want redirect url using .htaccess
like.
abc.website.com to website.com/folder/abc
http://abc.website.com to http://www.website.com/folder/abc
using .htaccess from PHP server
Use below rule,
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www|webmail|help|whm|root)
RewriteCond %{HTTP_HOST} ^(.+?)\.website.com
RewriteRule ^ http://www.website.com/folder/%1 [R=301,L]
Try putting this in your .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^abc.website.com
RewriteRule ^(.*)$ http://website.com/folder/abc/$1 [L,NC,QSA]
For a more general rule (that works with any subdomain, not just sub) replace the last two lines with this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.abc.website\.com
RewriteRule ^(.*)$ http://website.com/folder/%1/$1 [L,NC,QSA]
I hope it will work for you.

Wildcard redirect links going to root to subfolder on same domain

I have moved a website from www.example.com to www.example.com/subfolder and using an .htaccess file, I would like to redirect old links going to www.example.com to www.example.com/subfolder. My current .htaccess looks like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /subfolder/$1 [L]
I've also tried the following:
#RewriteCond %{REQUEST_URI} !^/subfolder/
#RewriteRule ^.*$ http://www.example.com/subfolder%{REQUEST_URI} [R=301,QSA,L]
I keep getting redirect loops or the redirect not working and leading to an error 404.
You can use this rule in site root .htaccess (parent directory of subfolder) as the first rule:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!subfolder/)(.*)$ /subfolder/$1 [L,NC,NE,R=301]
# remaining rules go here
Make sure to clear your browser cache completely when testing this change.
are you using wordpress? Or you just moving in general?
If you are moving in general this one-liner will work
RedirectMatch ^/$ /subfolder/
Original answer from here
This is another solution i'm using currently.
Do not copy and paste. You need to Replace DOMAIN (without the .com) and SUBFOLDER to the name u want.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/subfolder/
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.
# Rewrite all those to insert /folder
RewriteRule ^(.*)$ /subfolder/$1 [L]

Redirect from .htaccess does not work

I have a website and i want to make it always show as www.mysite .com i put that code in .htaccess :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
now the url shows as www but the website is not shown the browser said "This webpage has a redirect loop"
Note: .htcaccess do not have any code but this few lines i wrote
does i did anything wrong or i miss something??? please help
Edit:
before I add the above code the .htaccess had the following code but not direct to www i remove it :
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
RewriteCond %{QUERY_STRING} ^m=1$
RewriteRule (.*) $1? [R=permanent]
# END WordPress
could that be updated to solve my problem?
if you are doing it at windows server it will not work if it is window you have to create web.config file n for linux it should be .htaccess
Ok so Wordpress is also there. Make sure to do these 2 things:
Put your 301 rule above WP's rules i.e. just below RewriteBase line
Update WP settings to have your site address with www
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
or
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

URL Rewriting domain to use subdirectory as root; still shows that directory in URL in Analytics?

I set up a certain domain to rewrite into a folder, shown below, but have been having people show up in that folder in Google Analytics.
in short: /c/ -> root when from domain, but GA still shows domain/c/ in Visitors Flow
root .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?certain-domain.com$ [NC]
RewriteCond %{REQUEST_URI} !c/ [NC]
RewriteRule ^(.*)$ /c/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME}\.png -f
RewriteRule ^(.*)$ $1.png
RewriteCond %{REQUEST_FILENAME}\.txt -f
RewriteRule ^(.*)$ $1.txt
Options -Indexes
and /c/'s .htaccess is just an edited html5boilerplate one with the only difference being the same php and txt rewrite above.
But on Google Analytics, more than half of my visitors are still ending up on /c/index.php.
Visitor Flow: http://i.imgur.com/JwtcTFI.png
I haven't seen /c/ in my URL, so I really don't have a clue what's going on.
Any ideas? I do use relative URLs (is this a bad idea?) so that causes the flow to continue in the /c/ folder.
Thanks for the help!
Your rules do nothing to prevent people from directly accessing /c/whatever. If you want to prevent that, you need to add an extra rule:
RewriteCond %{THE_REQUEST} \ /c/
RewriteRule ^c/(.*)$ /$1 [L,R=301]
Oo, I found the answer! It seems like www.domain was redirecting to domain/c/index.php. I had a rewrite rule to remove the "www" in the /c/.htaccess, but not the root .htaccess.
I fixed it by adding code to remove the "www" in the root htaccess too BEFORE the domain>>directory rewrite. (after didn't seem to work)
I added
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
above
RewriteCond %{HTTP_HOST} ^(www.)?certain-domain.com$ [NC]
RewriteCond %{REQUEST_URI} !c/ [NC]
RewriteRule ^(.*)$ /c/$1 [L]
(in the root htaccess)
edit: It also probably has to do with the (www.), I dropped that due to redundancy. Now it's just RewriteCond %{HTTP_HOST} ^certain-domain.com$ [NC]
I also added a rewrite for /c/ to root in the /c/ .htaccess:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /c/
RewriteRule ^(.*)$ /$1 [L,R=301]

Internal Redirect - PHP and .htaccess?

Good evening,
I wanted to know if it is possible to redirect a request to my page as http://test.diogo.me to the page with the content -> http://www.diogo.me/pages/test/ (this redirection must be internal... the browser should point to http://test.diogo.me).
I have been reading about .htaccess and I feel that this is the resolution for me.
I have tried with the following code:
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} !^www\.diogo\.me [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.diogo\.me [NC]
RewriteRule ^(.*)$ /%1/$1 [L]
The output is error 404 file not found :(Am I going in the right track? Sorry about my expressing limitations. Thanks in advance
Just put this code in your .htaccess and remove your existing lines from there:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test\.diogo\.me$ [NC]
RewriteRule ^$ http://www.diogo.me/pages/test/ [L,P]
You were missing the /pages part
RewriteCond %{HTTP_HOST} !^www\.diogo\.me [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.diogo\.me [NC]
RewriteRule ^(.*)$ /pages/%1/$1 [L]
Assuming the subdomains have the same document-root as the main domain.

Categories