I know that there are a bunch of answers for this question already on this forum and over the internet. But everything I tried is not working for me and I have no clue why. So that's why I'm asking this question.
I'm running a website with ExpressionEngine CMS. To remove index.php from the URL there is a small .htaccess rule which mentioned below and working fine.
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Everything running fine with this code in the htaccess file. But now I want to force www. in the URL because some AJAX scripts are not working when the URL is without www.. So I found some threads on this forum:
.htaccess - how to force "www." in a generic way?
https://expressionengine.stackexchange.com/questions/9853/site-doesnt-work-with-www-at-the-start-of-the-address/9864#9864
https in htaccess and order of rules (using Expression Engine)
The last has the most clear solution in my opinion so I added this to my .htaccess file.
# Add www.
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
My .htaccess file now looks like this:
RewriteEngine on
# Add www.
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# remove index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
This however give's me a ERR_TOO_MANY_REDIRECTS error.
I tried all other kind of methods but I keep getting this error and I don't know why. Everything I try to force www. through htaccess doesn't work or gives me this error.
What is going on? Is it possible that DNS settings have to do something with this?
You need to change it on Plesk control panel, you access the domain in question and change the non-www to www or set it to none.
The option you look for is located at subscription > Web Hosting Settings > Preferred Domain.
In case you set it to none then you can use the .htaccess rule.
Related
I have been at this for a number of hours and cannot get this to work. I have other redirects in this .htaccess file that do work.
I need to redirect as follows.
mydomain.com/dir/subdir/myfile.php to myotherdomain.com/dir/my_index.php
Note that mydomain.com and myotherdomain.com both point to the same root directory.
Here's my code.
RewriteCond %{HTTP_HOST} ^(www.)?postle.com$
RewriteCond %{REQUEST_URI} !^/pi_www/hardface/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /pi_www/hardface/$1
RewriteCond %{HTTP_HOST} ^(www.)?hardfacetechnologies.com$
RewriteRule ^(/)?$ postle_hft/index_hft.php [L]
I have tired all the permutation of this I can think of and nothing works. I really would appreciate some help.
You can use this rule in /dir/subdir/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^myfile\.php$ http://myotherdomain.com/dir/my_index.php [R=302,NC,L]
If you want to avoid external redirect then:
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^myfile\.php$ /dir/my_index.php [NC,L]
should also work as both domains point to same site root.
In the .htaccess file in your document root, try:
RewriteCond %{HTTP_HOST} =mydomain.com
RewriteRule ^dir/subdir/myfile\.php$ http://myotherdomain.com/dir/my_index.php [R=302,L]
The order of directives is important. This should come after your canonical www redirect, but before any internal rewrites, and before any redirects that might conflict.
This also assumes that your canonical URL is the bare domain (ie. not the www subdomain) - like with the example in your question. To match the bare domain or the www subdomain (and make it case-insensitive) then change the RewriteCond directive to:
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain.com$ [NC]
Change the temporary (302) redirect to permanent (301) when you are sure it's working OK. (301 redirects are cached by the browser, so makes testing troublesome.)
Looked on here at all the answers regarding this issue but none fixes my issue. I have a directory called pdfs which used to contain all my .pdf files. They are now all inside pdfs/sales so trying to create a redirect for all files in the pdfs directory to look inside the pdfs/sales directory instead using a .htaccess file in the root of my site. Everything I've tried so far results in an infinite loop. Here's what my .htaccess looks like so far:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^pdfs/(.*)$ /pdfs/sales/$1 [R,NC,L]
RewriteRule ^(.*)$ /index.php?/$1 [L]
The first rule redirects all www. traffic to non-www. url. The second rule is my pdfs rule. The last rule is for redirecting all requests to index.php for seo friendly urls. Is there a conflict here? Where am I going wrong?
Thanks
You can keep your rule like this (my comment inline):
DirectoryIndex index.php
RewriteEngine on
# remove www from domain name
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# redirect /pdfs/abc.pdf to /pdfs/sales/abc.pdf
RewriteRule ^pdfs/((?!(?:sales|rent)/).*)$ /pdfs/sales/$1 [R=302,NC,L]
# for all non-files and no-directories route to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?/$1 [L]
Make sure to test it after clearing your browser cache.
I am taking over a site and I'm trying desperately to get our affiliate sites to stay at their rewrite paths. Instead, they all forward to the home page. I want 404s obviously to redirect and I know I am missing something here, but I've tried everything I can think of and I obviously need another pair of brains here. This is how it was written and I'm not sure how to edit it for what I need. How do I stop the affiliate pages from then forwarding to the index?? Seems like every edit I made breaks things!
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301]
RewriteRule ^aff/(.*)$ /index.php/aff/?aff=$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [R=301,NC]
ErrorDocument 404 /redirect/404_redirect.php
Redirect 301 /redirect/404_redirect.php http://privatemoneyexchange.org
I currently have my .htaccess file as the following:
ErrorDocument 404 /404.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
when I visit my primary site at: www.site.com, the www. is stripped as it should be and all works fine.
When I visit a subdomain in proper format (http://sub.site.com) everythign works fine.
The issue comes when a user would enter www. as part of a subdomain so:
http://www.sub.site.com does not strip the www. and instead redirects to the Media temple hosted "sub-domain does not exist" page (INCREDIBLY ANNOYING by the way).
Any suggestions?
Change these lines:
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
To:
RewriteCond %{HTTP_HOST} ^www\.(.+?)\.([^.]+)$ [NC]
RewriteRule ^(.*)$ http://%1.%2$1 [R=301,NC,L]
I believe I had to "trick" the system but found a working method:
I first had to create a vhost.conf file within my subdomain with:
ServerAlias www.subdomain.site.com
After resetting Plesk and my Apache server, I also added a wildcard DNS record to my subdomain as:
*.subdomain.site.com. A record with IP address appropriately
This seems to have resolved my issue for now though still can't figure out why the former did not work.
I have two domains, cshen.ca and cshen.net, both point to the same place.
I'm trying to accomplish the following with mod_rewrite:
cshen.net is 301 redirected to cshen.ca
www.cshen.net or www.cshen.ca are both redirected to cshen.ca
the path after the domain is preserved after being redirected, for example www.cshen.net/foo/bar/ would be redirected to cshen.ca/foo/bar/
I've tried a variety of rules, but can't seem to get it to work.
RewriteCond %{HTTP_HOST} ^cshen\.net$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://cshen.ca/$1 [R=301,L]
This accomplishes the first two rules, but redirects everything back to the home page, and does not preserve the rest of the URL.
RewriteCond %{HTTP_HOST} ^cshen\.net$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^/(.*)$ http://cshen.ca/$1 [R=301,L]
Making a small change and adding a slash '/' to the RewriteRule preserves the rest of the URL, but only www.cshen.ca is being redirected. Neither cshen.net nor www.cshen.net are being redirected anywhere.
I've also tried Apache's guide and used this code:
RewriteCond %{HTTP_HOST} !^cshen\.ca [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://cshen.ca/$1 [L,R]
I thought this would work, since it should redirect any url that isn't cshen.ca, however, like the second piece of code, it does nothing to cshen.net or www.cshen.net.
I've just about ran out of ideas of other things to try. I would appreciate it if someone could help out!
Addendum: in case it matters, I'm using WordPress's pretty URLs, and the rewrite rules for that are:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I've tried putting my rules before and after the WordPress rules (when I put it before I'd of course add RewriteEngine On before my rules), doesn't make any difference.
Thanks!
don't know how to edit, solved by placing the code from apache before WP's
must have forgotten to test this earlier.