I want to remove the www. from the front of my URL so I added the following code to .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
Which worked fine, but when people go to www.example.com/example it redirects them to the homepage.
Is there any way for it to just remove the www. from the page they are looking for then redirect them to that page instead of the homepage?
Just add $1 to the URL you redirect to, to capture the bracketed part in RewriteRule ^(.*)$ (i.e. the trailing example). I think something like this should do it:
RewriteRule ^(.*)$ http://%1$1 [R=301,QSA,NC,L]
Related
Using .htaccess, I want to redirect all the pages which contains --nocolor- at the end of their url. I want redirect them to the same url without the match.
Example:
https://www.test.com/products/test--nocolor-
to
https://www.test.com/products/test
What I've tried is
RewriteCond %{REQUEST_URI} (--nocolor-)
and that is working but I cannot get the right url while rewriting.
I've tried
RewriteRule ^products/([^/]*)(?=--nocolor-) /$1 [R=301,L]
RewriteRule ^(.+)(?=--nocolor-) /$1 [R=301,L]
RewriteRule ^.*(?=--nocolor-) /$1 [R=301,L]
but I always end up with $1 empty.
What do I do wrong?
With your shown samples, attempts please try following .htaccess rules. Make sure to place these rules at top of your .htaccess rules.
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(.*)--nocolor-\s [NC]
RewriteRule ^ /%1 [R=301,L]
I need to redirect anything with ?page= or ?page="" back to the homepage.
I tried RewriteRule ^/\?page=(.*)$ / [R=301,L] but it doesn't work.
Example:
http://example.com/?page= or http://example.com/?page=test or
http://example.com/?page="test"
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) /404.php? [R,L]
Did the trick.
The RewriteCond part catches any query (.) and then the RewriteRule redirects it to a 404 page.
I need to have a .htaccess redirect.
If I go to example.bla.org
I want it to redirect to bla.org/example
EXCEPT: It must keep the subdomain url can needs to be dynamic.
Ex: example.bla.org/apple -> bla.org/example/apple
I have tried almost every method like this one:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^examples.website.org [NC]
RewriteRule ^/(.*)$ /examples/$1 [L]
Please help, thanks!
You're pretty close, try this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.website.org [NC]
RewriteRule ^(.*)$ http://website.org/example/$1 [L]
If you want to do a 301 redirect for SEO etc, add R=301 to the rewrite :
RewriteRule ^(.*)$ http://website.org/example/$1 [R=301,L]
I have an issue removing the www from an URL. Also I want to use directory-like URL's (e.g. When I type www.foo.com/bar/baz into URL bar, it is converted into foo.com/index?page=bar&action=baz in the background and only in the background). But what I'm getting is that when I type in the URL with www and hit an Enter, I get back a foo.com/index?page=bar&action=baz in the URL bar. I only want foo.com/bar/baz to appear. (If I already wrote the URL without www, it's working fine.)
This is my code (only changed the original URL's):
RewriteCond %{HTTP_HOST} ^www\.foo\.com$ [NC]
RewriteRule (.*) http://foo.com$1 [L,R=301]
RewriteCond %{REQUEST_URI} !/index\.php
RewriteCond %{REQUEST_URI} !/Views/.*\.js
RewriteCond %{REQUEST_URI} !/Views/.*\.css
RewriteRule ^([^/]+)/([^/]+)(/[^/]+)* /index.php?page=$1&action=$2 [L,QSA]
RewriteCond %{REQUEST_URI} !/index\.php
RewriteCond %{REQUEST_URI} !/Views/.*\.js
RewriteCond %{REQUEST_URI} !/Views/.*\.css
RewriteRule ^([^/]+) /index.php?page=$1 [L,QSA]
So my question is obviously: What am I doing wrong and how can I make it work?
There may be some other Apache rules in play but from the look of it there may be an issue with slashes. It would be better if your first rule was this --
RewriteRule (.*) http://foo$1 [L,R=301]
Without the trailing slash. The reason for this is you will create a double // when redirecting www.foo.com/foo/bar. It will redirect to foo.com//foo/bar.
Your other rules should start with /, e.g. --
RewriteRule ^/([^/]+)/([^/]+)(/[^/]+)* /index.php?page=$1&action=$2 [L,QSA]
And --
RewriteRule ^/([^/]+) /index.php?page=$1 [L,QSA]
I'm using following htaccess code to redirect mobile users
# Mobile Redirection
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^$ http://m.mydomain.com [R,L]
Problem is it works only ehen user type http://mydomain.com/
If I type http://mydomain.com/anyurl it will not redirect to subdomain. Pls help.
You are matching the root url with the regex ^$.
Try ^.*$, and if you want to redirect to the relevant mobile page, the line should be RewriteRule ^(.*)$ http://m.mydomain.com/$1 [R,L] ($1 is the matched pattern in brackets).
Change your rewrite rule to the following:
RewriteRule ^.*$ http://m.mydomain.com [R,L]