Search code examples
apache.htaccessmod-rewriteurl-rewritingfriendly-url

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


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.


Solution

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