2

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?

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
Ann
  • 23
  • 4
  • in .htaccess you have mentioned specialities where as in your url you have added speciality. Your rewrite rule is not matching your URL hence you are getting 404 error – Bhavik Dec 21 '22 at 06:54
  • @Bhavik Sorry I didn't get you can you please explain – Ann Dec 21 '22 at 06:59
  • `RewriteRule ^/specialities/([0-9]+)/([^/.]+)$` - this is your rewrite rule which you have written. So your URL should be `example.com/specialities/23/ent` OR your RewriteRule should be `RewriteRule ^/speciality/([0-9]+)/([^/.]+)$` – Bhavik Dec 21 '22 at 07:02
  • thank you I have changed to example.com/specialities/23/ent this but still, it shows not found – Ann Dec 21 '22 at 07:29
  • Please update/edit your question with the changes you have made to the URL (otherwise other readers are going to see the same mistakes). Also please include your complete .htaccess file with these directives in place (it’s likely you have a conflict). – MrWhite Dec 21 '22 at 11:56

1 Answers1

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

MrWhite
  • 43,179
  • 8
  • 60
  • 84