Home > OS >  The requested URL was not found on this server .htacess
The requested URL was not found on this server .htacess

Time:12-27

I am trying to rewrite URL like:

example.com/speciality_details.php?id=23&name=ent

TO

example.com/specialities/23/ent

But I am getting this error:

Not Found
The requested URL was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

This is my .htaccess file

RewriteEngine on
RewriteRule ^doctor/([0-9] )/([^/.] )$ doctor_details.php?id=$1&name=$2 [NC,L] 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/specialities/([0-9] )/([^/.] )$ speciality_details.php?id=$1&name=$2 [NC,L] 

The first RewriteRule working but the second one is not working

Please help me to know what the problem is. How should I rewrite the code?

CodePudding user response:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/specialities/([0-9] )/([^/.] )$ speciality_details.php?id=$1&name=$2 [NC,L]

Just as in the first rule (which is "working"), you should not be matching a slash prefix on the URL-path. And the preceding condition (RewriteCond directive) is superfluous, since a URL of the form /specialities/23/ent could not possibly match a physical file (could it?).

In .htaccess, the URL-path matched by the RewriteRule pattern does not start with a slash since the directory-prefix (that always ends with a slash) has already been removed.

So, the rule should look like the following instead (and no RewriteCond directive):

RewriteRule ^specialities/([0-9] )/([^/.] )$ speciality_details.php?id=$1&name=$2 [NC,L]

This would match a URL of the form example.com/specialities/23/ent, as per your example. And assumes the file being rewritten to is speciality_details.php in the document root.

The NC (nocase) flag should also be superfluous, unless you are expecting mixed case versions of sPeCiAlItIeS? But if you are then that is better resolved with a redirect since the rewrite would potentially result in a duplicate content (SEO) issue.

Make sure you clear your browser cache before testing.

Although, from your earlier question edits it looks like you had already tried this without the slash prefix, but at the time you had /speciality/23/ent, not /specialities/23/ent as the example request URL - which would obviously not match.

  • Related