I am trying to accomplish the following but my code is not working
1) I want to do either non-www to www or www to non-www
2) desktop users should be redirected to example.com/homepage
3) remove or hide /homepage from url
4) redirect mobile users to example.com/m
here is my code, it's not working, too many problems to list, but off the top of my head, I can think of 2 problems that I encountered: "cannot open page because too many redirects occurred" and mobile users are taken to /homepage instead of /m
# remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]
RewriteEngine On
RewriteRule ^$ /homepage [L]
RewriteEngine On
RewriteRule ^$ homepage/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ homepage/$1
RewriteCond %{HTTP_USER_AGENT} ^.*(ip(ad|od|hone)|blackberry|iemobile|android).*$ [NC]
RewriteCond %{REQUEST_URI} !^/m/.*
RewriteRule ^.*$ http://example.com/m [R=301,L]
Please help me, I am so frustrated, what am I supposed to change about this code to get it to work?
Unfortunately im no genious with regex or redirects in htacces but I have gotten similar code to work myself.
It does sound like your code is getting stuck in a redirect loop and the browser is stopping it from continuing.
I know thats not a complete answer but Hopefully that helps you make more sense of the code atleast a little.
Hopefully someone else can chime in otherwise i can dig up the code that is working when im on the computer and hopefully give you a better response.
-Sent from my cell
The www/non-www is fine.
For the homepage stuff, you need to do two things: make sure what you're rewriting into the homepage directory actually exists (otherwise you get a 500 server error) and exclude mobile user agents.
So this stuff:
RewriteEngine On
RewriteRule ^$ /homepage [L]
RewriteEngine On
RewriteRule ^$ homepage/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ homepage/$1
Needs to look like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_USER_AGENT} !^.*(ip(ad|od|hone)|blackberry|iemobile|android).*$ [NC]
RewriteCond %{DOCUMENT_ROOT}/homepage%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/homepage%{REQUEST_URI} -d [OR]
RewriteRule ^(.*)$ homepage/$1 [L]
Related
I want to redirect the url of http://sitename/modules/fb/fb.php to http://sitename/modules/take_control .How can i do that using htaccess
I tried like
RewriteRule ^/?modules/fb/fb\.php$ take_control , Is that right ?
Your solution is the opposite of what you most likely want if you are trying to rewrite to take_control.
Give this a try. These rules should provide the rewrite with take_control as the URI both ways.
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\ /+modules/fb/fb\.php [NC]
RewriteRule ^ /take_control/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^take_control/?$ /modules/fb/fb.php [L]
I have the following url
http://mywebsite.com/browse-test.php?v=myname
I want it to be redirected to this instead
http://mywebsite.com/myname
This is what I tried so far
RewriteEngine On
RewriteCond %{HTTP_HOST} mywebsite.com/browse-test.php?v=$ [NC]
RewriteRule ^(.*)$ http://mywebsite.com/$1 [L,R=302]
When I looked for an answer on this website and google I didn't find any examples that made a redirect based on on a specific file and php url paramater.
This code should help.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /+browser-test\.php\?v=([^&\s]+)
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /browse-test.php?v=$1 [L]
Having a look at the mod_rewrite docs to get a better understanding as MrTux noted is highly suggested to better understand the different options.
I changed .htacess file as below.
RewriteCond %{REQUEST_URI} (.*)product-list.php(.*)
RewriteRule (.*) www.example.com/swimming-pool/product-list\.php$1 [R=301,L]
RewriteCond %{REQUEST_URI} (.*)product-info.php(.*)
RewriteRule (.*)\?(.*)$ www.example.com/swimming-pool/product\-info\.php$2 [R=301,L]
i just need that when i request for
http://www.example.com/product-info.php?Applepc.html should be redirected to
http://www.example.com/swimming-pool/product-info.php?Applepc.html
Output is getting like this in URL field:-
www.example.com/swimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpswimming-pool/product-list.phpproduct-list.php?Flowers-pg1-cid38.html
Please tell me where i am mistaking.
Although you don't mention what is the purpose of the other rule with product-list.php, it is included in this rule-set.
You may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (product-list|product-info)\.php [NC]
RewriteRule ^(.*)/?$ swimming-pool/$1 [R=301,L]
Redirects permanently any URL like this one
http://www.example.com/product-info.php?query or
http://www.example.com/product-list.php?query
To
http://www.example.com/swimming-pool/product-info.php?query or
http://www.example.com/swimming-pool/product-list.php?query
It seems the problem with the actual rules is they are generating a loop.
I have a folder structure on my server like so:
.htaccess
index.php
[website1]
[placeholder]
index.php
[website2]
[website3]
All domains web1.com, web2.com and web3.com link to this directory. I like the htaccess to route them to the proper directory. So web1.com goes to website1, web2.com goes to website2, etc.
However I like the URL to be something like http://www.web1.com/ and it should show the content in website1.
Now I have the following code:
RewriteCond %{HTTP_HOST} ^(web1.com)$ [NC]
RewriteRule ^(.*)$ http://www.web1.com/$1
RewriteCond %{HTTP_HOST} ^(www.web1.com)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /website1/$1
RewriteCond %{HTTP_HOST} ^(www.web1.com)$ [NC]
RewriteRule ^(/?|index.php)$ website1/index.php [L]
This works, however if you go to http://www.web1.com/placeholder it will display the following in the URL:
http://www.web1.com/website1/placeholder/
Of course it's preferred to see the URL as http://www.web1.com/placeholder. For the record http://www.web1.com/placeholder/ does show the proper page.
How can I fix this? It's driving me nuts.
EDIT (ANSWER):
RewriteCond %{HTTP_HOST} ^(www.web1.com)$ [NC]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^website1/(.*)([^/])$ http://%{HTTP_HOST}/$1$2/
Not sure if this is the proper way to do it, but it seems to work.
How can I redirect requests to mydomain.tld/somepage.ext to mydomain.tld/mydomain/somepage.ext? I have subdomains like subdomain.mydomain.tld that I don't want to be affected by this.
I can't seem to get it to work right. I'm trying this:
RewriteEngine on
RewriteCond %{HTTP_HOST} mysite.com
RewriteCond %{REQUEST_URI} !^/mysite
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /mysite/$1
But it isn't redirecting anything at all.
I also want to exclude one or two folders from this rule.
This seems to be working...
RewriteEngine on
RewriteCond %{HTTP_HOST} mysite\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mysite/ # but I don't see why this line is needed when I've used [L] below
RewriteCond %{REQUEST_URI} !^/ignored_folder/
RewriteRule .* /mysite/$0 [QSA,L]
At least for now.