404 issue in two htaccess in root domain and subfoler - .htaccess

My root domain htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
ErrorDocument 404 /404.php
RewriteCond %{`HTTP_HOST`} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ `https://%1/$1` [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) `https://%{HTTP_HOST}%{REQUEST_URI}` [R=301,L]
In my subfolder htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} `^(www\.example\.com)?$`
RewriteRule ^(.*)$ `https://example.com/subfolder/$1` [R=301,L]
Wehn I type in example.com/any-wrong-url, it will redirect to 404 page, that is correct.
But when I type in example.com/subfoler/any-wrong-url it can not redirect to 404 page. Where do I something wrong?

The best way to do this without creating any conflict or redirect is:
Use /subfolder/ instead of root / on the RewriteBase like this:
RewriteBase /subfolder/
Copy the 404.php inside the subfolder.
The complete code will look like:
RewriteEngine On
RewriteBase /subfolder/
ErrorDocument 404 /404.php
RewriteCond %{HTTP_HOST} `^(www\.example\.com)?$`
RewriteRule ^(.*)$ `https://example.com/subfolder/$1` [R=301,L]
This should work.

Related

htaccess redirect for url with query string

I want to redirct this url http://www.example.com/tev/?user-register=registered
to the home page http://www.example.com/
I have the following code in my htaccess file but it isn't working
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^user-register=registered$
RewriteRule .* http://www.example.com/ [R=301,L]
Try with:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^user-register=registered$
RewriteRule ^tev/?$ http://www.example.com/? [NC,R=301,L]

.htaccess 301 Redirect all content except for subdirectory

I want to redirect example.com to example2.com, except for example.com/wp-admin
How do I redirect all pages on example.com to example2.com except for the subdirectory example.com/wp-admin?
I've tried:
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^wp-admin/$ [NC]
RewriteRule (.*)$ https://example2.com/$1 [R=301,L]
</IfModule>
but this does not work.
Help appreciated.
You can use this rule in site root .htaccess of example.com site:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule !^wp-admin/ http://example2.com%{REQUEST_URI} [NE,R=301,L,NC]

.htaccess redirection problems

I am creating a website using 000webhost.com. This is set up so that mysite.com goes to www.mysite.com, and mysite.com/page goes to mysite.com/under_construction/page. A few problems:
mysite.com goes to www.mysite.com/under_construction instead of www.mysite.com (although www.mysite.com does show the contents of under_construction)
mysite.com/invalidpage goes to the default error page instead of error.mysite.com/404.
error.mysite.com shows up just fine, but www.error.mysite.com brings me to the default error page.
I have the following DNS zone record set up:
www.bossgamerz.cu.cc CNAME bossgamerz.cu.cc
RewriteEngine On
#Options +FollowSymLinks
ErrorDocument 404 http://www.error.bossgamerz.cu.cc/404
ErrorDocument 403 http://www.error.bossgamerz.cu.cc/403
RewriteBase /
RewriteCond %{HTTP_HOST} www.bossgamerz.cu.cc
RewriteCond %{REQUEST_URI} !under_construction/
RewriteRule ^(.*)$ under_construction/$1 [L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.bossgamerz.cu.cc/$1 [R=301,L]
I do not neccesarily need the error messages to be in an error subdomain.
Your rules seem to be not in right order and you need to exclude error pages from rewrites. Try this code:
Options +FollowSymLinks
ErrorDocument 404 /404
ErrorDocument 403 /403
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.bossgamerz.cu.cc%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{REQUEST_URI} !^/(under_construction|404|403)
RewriteRule ^ under_construction [L]

htaccess adding query string to 301 redirect

Can sombody help me out with this, im trying to re direct a page using htaccess file but it keeps adding ?c=oldpage on to the end of the new url, example:
http://www.mydomain.co.uk/newpage.html?c=oldpage
i have tried some of the solutions posted here but no luck, here is my .htaccess file:
DirectoryIndex index.php index.html index.htm
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} PHPSESSID=.*$
RewriteRule (.*) http://www.mydomain.co.uk/$1? [R=301,L]
RewriteCond %{HTTP_HOST} ^mydomain.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.co.uk/$1 [R=301,L]
RewriteCond %{THE_REQUEST} /index\.php\ HTTP/
RewriteRule ^index\.php$ / [R=301,L]
RewriteEngine on
RewriteRule ^product/(.*).html$ product.php?p=$1 [L]
RewriteRule ^(.*)\.html$ category.php?c=$1 [L,NC]
Redirect 301 /oldpage.html http://www.mydomain.co.uk/newpage.html
ErrorDocument 404 /404.php
Thanks for any help.
This is mod_alias (the Redirect directive) and mod_rewrite not playing nicely with each other. Because both modules apply their directives on the same URI in the URL-file mapping pipeline, they don't know to ignore each other since neither directive knows what the other module is doing. Since you're targets overlap, both modules are applying their directives on the same URI and you get a mish-mashed result.
You need to stick with mod_rewrite in this case and move the redirect above the internal rewrites:
DirectoryIndex index.php index.html index.htm
Options +FollowSymLinks
RewriteEngine on
# redirects
RewriteCond %{QUERY_STRING} PHPSESSID=.*$
RewriteRule (.*) http://www.mydomain.co.uk/$1? [R=301,L]
RewriteCond %{HTTP_HOST} ^mydomain.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.co.uk/$1 [R=301,L]
RewriteCond %{THE_REQUEST} /index\.php\ HTTP/
RewriteRule ^index\.php$ / [R=301,L]
RewriteRule ^oldpage.html$ http://www.mydomain.co.uk/newpage.html [R=301,L]
# internal rewrites
RewriteRule ^product/(.*).html$ product.php?p=$1 [L]
RewriteRule ^(.*)\.html$ category.php?c=$1 [L,NC]
ErrorDocument 404 /404.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^abc\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.abc\.net$
RewriteRule ^example\.html$ "http\:\/\/abc\.net\/example\/" [R=301,L]
RewriteOptions inherit
to a folder, or:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^abc\.net$ [OR]
RewriteCond %{HTTP_HOST} ^www\.abc\.net$
RewriteRule ^example\.html$ "http\:\/\/abc\.net\/example\.html$" [R=301,L]
RewriteOptions inherit
Don't know much about it, but it's what i use, hope it helps.

Codeigniter url rewrite like subdomain url with .htaccess

Currently my Url is: http://www.domain.co.uk/index.php/city/details/city-name
I would like to change it to:
http://www.city-name.domain.co.uk/index.php/city/details/city-name
or:
http://www.city-name.domain.co.uk/city/details/city-name
Put the .htaccess file into the http ://www.domain.co.uk/ document root
To http ://www.city-name.domain.co.uk/index.php/city/details/city-name
RewriteRule ^(.*)/([^/]+)$ http://www.$2.domain.co.uk/$1/$2 [R=301,L]
To http ://www.city-name.domain.co.uk/index.php/city/details/city-name
RewriteRule ^index.php/(.*)/([^/]+)$ http://www.$2.domain.co.uk/$1/$2 [R=301,L]
If the server is the same, set above RewriteRule this line to prevent redirection loop
RewriteCond %{HTTP_HOST} !^www\.(.*).domain\.co\.uk [NC]
File content example
<ifModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.(.*).domain\.co\.uk [NC]
RewriteRule ^index.php/(.*)/([^/]+)$ http://www.$2.domain.co.uk/$1/$2 [R=301,L]
</IfModule>
To exclude domain.co.uk (whitout www)
<ifModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.(.*).domain\.co\.uk [NC]
RewriteCond %{HTTP_HOST} !^domain\.co\.uk [NC]
RewriteRule ^index.php/(.*)/([^/]+)$ http://www.$2.domain.co.uk/$1/$2 [R=301,L]
</IfModule>

Resources