301 Rewrite redirecting to wrong place - http-status-code-301

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

Related

Force www. in URL name through .htaccess

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.

Redirect a subdomain to a dynamically generated folder and mask theurl

I currently have a blog setup as the subfolder of the main site, mydomain.com.au/blog/, however the /blog is dynamically generated through PHP and doesn't physically exist.
I have created a subdomain blog.mydomain.com.au and I am trying to get it to display everything just like mydomain.com.au/blog/ but mask the url so that it shows blog.mydomain.com.au.
I have made several attempts using htaccess and got close a couple of times, but there's always something wrong. Below are the 2 attempts which got me the closest to the expected results:
1st attempt
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^blog\.mydomain\.com\.au$
RewriteRule ^.*$ http://www.mydomain.com.au/blog%{REQUEST_URI} [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
This will get blog.mydomain.com.au to redirect to www.mydomain.com.au/blog/, but does not mask the URL (the address bar will show www.mydomain.com.au/blog/).
2nd attempt
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com\.au$ [NC]
RewriteRule blog/(.*) http://blog.mydomain.com.au/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
This got me the closest. It redirects and masks the URL just the way I want it to for all pages under /blog/ (e.g. blog.mydomain.com.au/whatever/ redirects to www.mydomain.com.au/blog/whatever/ but masks the URL as blog.mydomain.com.au/whatever/), but it doesn't do it for blog.mydomain.com.au, which gets redirected to www.mydomain.com.au.
You have two different requirements here. The first one is a rewrite, which will work, if the request is blog.mydomain.com.au and you remove the domain part and the R flag in the rule substitution
RewriteCond %{HTTP_HOST} ^blog\.mydomain\.com\.au$ [NC]
RewriteRule !^blog/ /blog%{REQUEST_URI} [L]
The second one is an actual redirect, if the request is www.mydomain.com.au/blog/
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com\.au$ [NC]
RewriteRule blog/(.*) http://blog.mydomain.com.au/$1 [R,L]
When everything works as you expect, you can change R to R=301.
Never test with 301 enabled, see this answer
Tips for debugging .htaccess rewrite rules
for details.

Rewrite Rule custom index file Drupal installation

This is the current Rewrite rule of Drupal set in my htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteCond %{REQUEST_URI} !=/index.html
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
This works well, but the problem is that I want to have a custom page which will open when people browse to the website. More specific:
www.company.com/ should open index.html
All other pages should use the default rewriterule of Drupal.
I tried editing the DirectoryIndex, but then all pages are redirected to index.html.
DirectoryIndex index.html index.php
I also tried adding different Rewrite Rules but I didn't have any luck yet. Htaccess is not my best skill.
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteRule ^/$ /index.html [L]
Probably there will be an easy solution, but I can't figure out what to do.
Does anyone know how to solve my problem?
Thanks!
Try adding to the rule the 301 code, meaning permanently moved.
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteRule ^/$ /index.html [L,R=301]

Is this htaccess rewrite correct?

I have the following in my htaccess file. What I'm trying to do is to make
domain.com/index.php/view/whatever accessable via domain.com/whatever
and also redirect from non www to www.
This works for all urls that have index.php/view in them but now other URLs that don't have index.php/view in them are breaking not working. Ex: domain.com/index.php/site/pages no longer works since it doesn't have index.php/view in it.
I want htaccess to only affect those URLs that have index.php/view in them and not anything else. What do I need to do to fix that?
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/view/$1 [L]
RewriteCond %{http_host} ^domain.com [nc]
RewriteRule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
UPDATE. To narrow things down, How can i have both rules like so. i need them both
RewriteRule ^(.*)$ /index.php/view/$1
RewriteRule ^(.*)$ /index.php/site/$1
So if a request is made to something like http://domain.com/index.php/site/pages, you want it to pass through untouched, but if it's something like http://domain.com/whatever, you want it to get rewritten?
The first rule you have there matches everything (except files and directories). You probably want to narrow the RewriteRule with something like:
RewriteRule !^index.php /index.php/view/$1 [L]
HTH
Neal

.htaccess mod_rewrite redirection between domains

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.

Resources