Muhammad Usman
Muhammad Usman

Reputation: 1423

rewriting htaccess for a specific url and redirect to 404 error

My site uses rewrites and urls such as this one:

http://example.com/profile/199801173

However, when someone does something like:

http://example.com/profile/199801173kjsjsk

It still redirects to the page of the prior link.

How can I fix this rewrite rule and make it go to a 404 page instead of going to the profile page?

ErrorDocument 404 /404.php

my profile url was in query string i rewrite it via htaccess

RewriteRule ^profile/([^/]*)$ /profile.php?user=$1 [L]

Kindly help me how i solve this problem

Upvotes: 2

Views: 1511

Answers (1)

eykanal
eykanal

Reputation: 27077

Your regular expression simply looks for any string that doesn't include a /.

  • The [^/] segment means "anything that's not a /"
  • The * following that means "look for 0 or more of the preceeding"
  • The final $ means "end of string")

Put together, that's "look 0 or more of any character except /, followed by an end of string". From the example you posted, it looks like you only want to check for an arbitrary number of digits. You can achieve that as follows:

RewriteRule ^profile/([0-9]*)$ /profile.php?user=$1 [L]

This means "look for any number of digits between 0 and 9, followed by an end of string".


EDIT: Based on the comments, it looks like you want to restrict to a specific number of digits. As described above, the * means "any number". You can replace that with {n} for just n digits, or {m,n} for between m and n digits, inclusive. E.g., for seven digits:

RewriteRule ^profile/([0-9]{7})$ /profile.php?user=$1 [L]

Upvotes: 1

Related Questions