RewriteRule (contact.php -> contact) + 404 Redirection gives me always a 404 page - apache

Recently i Modified my .htaccess file in my apache server. and this is the following code
RewriteEngine On
<IfModule mod_speling.c>
CheckSpelling On
</IfModule>
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^course/([^/.]+)$ course.php?name=$1 [QSA,L]
RewriteRule ^course/([^/.]+)/$ course.php?name=$1 [QSA,L]
RewriteRule ^about$ about.php [QSA,L]
RewriteRule ^about/$ about.php [QSA,L]
RewriteRule ^contact$ contact.php [QSA,L]
RewriteRule ^contact/$ contact.php [QSA,L]
RewriteRule ^downloads$ downloads.php [QSA,L]
RewriteRule ^downloads/$ downloads.php [QSA,L]
RewriteRule ^ errors/404.html [L,NC]
Now the Problem is
If I I enter the URL like http://localhost/about in my web browser it well always shows the custom 404.html
I think the problem is on the last line.
If i remove the last line it will works.
But I also want the 404 page. Like If the user enters http://localhost/SomeRandomString the 404 page show on this url.
I also tried ErrorDocument on .htaccess file but it will change the url
Is any solution for this problem.

Have your htaccess rules file in following way. Place it in your root directory, make sure to clear your browser cache before checking your URLs.
RewriteEngine On
<IfModule mod_speling.c>
CheckSpelling On
</IfModule>
##Rules that cover your uris starting with course here.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(course)/([^/.]+)/?$ $1.php?name=$2 [NC,QSA,L]
##Rules that cover your uris starting with about/contact/downloads here.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(about|contact|downloads)/?$ $1.php [NC,QSA,L]
##Rules that cover all uris which aren't matching any of the above conditions.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ errors/404.html [L,NC,QSA]

Related

Complex .htaccess rewrite

can you please let me know htaccess code to redirect there urls:
https://www.example.com/forum/news/state-news/archive to https://www.example.com/forum/news/state-news?archive=yes
https://www.example.com/forum/news/state-news/city-news/archive to https://www.example.com/forum/news/state-news/city-news?archive=yes
https://www.example.com/forum/news/state-news/city-news/area-news/archive to https://www.example.com/forum/news/state-news/city-news/area-news/?archive=yes
My tried htaccess file is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(core/libraries/|includes|core/includes|core/vb|core/vb5|core/admincp|core/modcp) index.php?routestring=relay/404 [END]
RewriteRule ^css.php$ core/css.php [NC,L]
RewriteRule ^install/ core/install/ [NC,L]
RewriteCond %{REQUEST_URI} !\.(gif|jpg|jpeg|png|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?routestring=$1 [L,QSA]
</IfModule>
Have your htaccess Rules file in following way. I have specifically covered 3 URLs shown by you in your question. Please make sure you use these Rules and make sure you clear your browser cache before testing your URLs.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(core/libraries/|includes|core/includes|core/vb|core/vb5|core/admincp|core/modcp) index.php?routestring=relay/404 [END]
RewriteRule ^css\.php$ core/css.php [NC,L]
####I have commented below rule that doesn't make sense, since NO right part in it only condition is mentioned.
####RewriteRule ^install/core/install/ [NC,L]
###New rules added by me to handle 3 different URLs shown by OP in question.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(forum/news/state-news(?:(?:/city-news(?:area-news))))/(archive)/?$ $1?$2=yes [NC,QSA,L]
RewriteCond %{REQUEST_URI} !\.(gif|jpg|jpeg|png|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?routestring=$1 [L,QSA]
</IfModule>

How to redirect URL into a new URL using .htaccess?

I want to redirect pages using .htaccess. I am using code igntiter
When user type this URL in addresss bar :
form.bixy.com/salesapp
and they hit enter button, the URL should changed into:
form.bixy.com/salesapp/administrator/addusr
I have this .htaccess inside the salesapp folder
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|images|stylesheets|system/application/sources/)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/
</IfModule>
If the line RewriteRule ^(.*)$ index.php?/ is removed, I got The requested URL was not found on this server.
I have little knowledge about .htaccess. Any help is greatly appreciated, thanks in advance.
Edit:
This is my current .htaccess that I put inside salesapp folder:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|images|stylesheets|system/application/sources/)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php [L]
RewriteRule ^/? /salesapp/administrator/addusr[NC,R=301,L]
</IfModule>
It still give me error, but the URL should've worked
The page isn’t redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
Inside salesapp you can try this .htaccess:
RewriteEngine on
RewriteBase /salesapp/
RewriteRule ^(?:administrator)?/?$ administrator/addusr [R=301,L,NC]
RewriteCond $1 !^(index\.php|images|stylesheets|system/application/sources/) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php [L]

error 404 redirect not working with my .htaccess

I tried using 
ErrorDocument 404 /abc/404.php
But when the wrong URL is entered it redirects to the main page, not to the 404 error page
I have a htaccess file with this code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?search=([^\s&]+) [NC]
RewriteRule ^ search/%1? [R=302,L,NE]
RewriteRule ^search/(.*)/page/(.*)/?$ search.php?search=$1&page=$2 [NC,L]
RewriteRule ^search/(.*)$ search.php?search=$1 [L,QSA,NC]
RewriteRule ^category/(.*)/page/(.*)/?$ category.php?id=$1&page=$2 [NC,L]
RewriteRule ^category/(.*)$ category.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
ErrorDocument 404 /abc/404.php
These two rules...
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
RewriteRule ^(.*)$ index.php/$1 [L]
Replace any URL that is accessed with the replacement... Unless the accessed URL is a real file/directory. As per the RewriteCond rules preceding them:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
If you don't want to divert every request that isn't a real file/directory then you need to change those rules...

Codeigniter .htaccess not making my urls correct

I have following .htaccess file
<IfModule mod_rewrite.c>
# Checks to see if the user is attempting to access a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
I am on localhost with WAMP installed on it. Normally this htaccess work perfectly but this time it isn't I have to mention index.php in my URLs. I have checked mod_rewrite is enabled in Apache. I just want to know what I missed
Try this...
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]

strange redirect with url rewrite

My .htaccess file is:
RewriteEngine on
RewriteRule (.*) index.php?_url=$1 [QSA,L]
It works perfectly in most cases. But if the requested url exists as a real folder, it goes crazy.
For example when i request /random it is rewritten to index.php?_url=random with status code 200, but for /static (which is an existing directory on the web server) it gives a 301 redirect to /static/?_url=static
use this below code for .htaccess
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
If you want to exclude existing files and directories from being rewritten, do so with a RewriteCond
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?_url=$1 [QSA,L]