Redirect url contains both numbers and letters with Htaccess - .htaccess

Im trying to Rewrite my url with this htaccess code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
ErrorDocument 404 mysite
RewriteRule ^([a-z-]+)$ /name.php?n=$1 [NC]
RewriteRule ^([0-9]+)$ /age.php?e=$1 [NC]
</IfModule>
when i tried open link mysite/abc or mysite/20. Both of them are working but if try this mysite/abc123 or mysite.com/a2b2c the page show error message. This page isn’t working
How can i redirect those link mysite/abc123 , mysite/a2b2c to my home page mysite with htaccess ?
Thank you!

You may try these rules:
ErrorDocument 404 /
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteRule ^([a-z-]+)/?$ name.php?n=$1 [L,QSA,NC]
RewriteRule ^([0-9]+)/?$ age.php?e=$1 [L,QSA,NC]
ErrorDocument 404 / will forward all not found URLs to home page without changing the URL in browser. If you want to redirect those 404 URLs and want to change them to home (landing) page then use:
ErrorDocument 404 https://example.com/

Related

How to redirect all not found page to an error page

Options -MultiViews
RewriteEngine on
ErrorDocument 404 /error.php
RewriteRule ^index.html$ / [R=301,L]
RewriteRule ^(.*)/index.html$ /$1/ [R=301,L]
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example.com [NC]RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
RewriteRule ^news/([0-9a-zA-Z_-]+)$ /news.php?url=$1 [NC,L]
RewriteRule ^category/([0-9a-zA-Z_-]+)$ /category.php?kategori=$1 [NC,L]
on my .htaccess document
It is working and redirecting to the error page the page when i type www.example.com/hdjskahdjska
But when i typed www.example.com/news.phpdsadsadsa it doesn't redirect to the error page, but it shown me a multiple choice page whether to choose news.php or news.html
And when i typed www.example.com/news/dsadsadsa it shown my page news without any content on it, how can i also redirect that url to an error page ?
I also want when someone typed www.example.com/index.php or www.example.com/index.html to redirect to the error page.
I need your help please Thank you

404 issue in two htaccess in root domain and subfoler

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.

HTTP to HTTPS redirect using .htaccess file not working

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
ErrorDocument 404 https://whatmovieshouldiwatchtoday.com/error404.php
php_flag display_errors 1
RewriteEngine On
RewriteRule ^/?movie/([0-9A-Za-z-:-]+)/?$ /movie.php?id=$1 [NC,L]
RewriteEngine On
RewriteRule ^/?cast/([0-9A-Za-z-:-]+)/?$ /cast.php?cast=$1 [NC,L]
RewriteEngine On
RewriteRule ^/?keyword/([0-9A-Za-z-:-]+)/?$ /keyword.php?keyword=$1 [NC,L]
This is my code, now this part of the code is supposed to redirect HTTP to https, but it is not working:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
When I try to open my website (even with https), Google chrome shows "Waiting For Whatmovieshouldiwatchtoday.com" and after few seconds, it shows
This page isn’t working
whatmovieshouldiwatchtoday.com redirected you too many times.
Try clearing your cookies.
ERR_TOO_MANY_REDIRECTS

.htaccess rewrite index GET request

So, I want to rewrite my url from this:
http://example.com/l1
to
http://example.com/?loc=l1
Currently I have this:
RewriteEngine On
RewriteRule ^/(.*)$ ./?loc=$1 [NC,L]
ErrorDocument 404 /errors/404.php
But each time I type http://example.com/l1 it gives me a 404 error! Please help!
Use this .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /?loc=$1 [NC,L]
ErrorDocument 404 /errors/404.php
I do not rewrite the links to existing files with !-f.
And URI in RewriteRule does not begin with a /

.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]

Resources