Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewrite Rule To Detect Numbers Only

I am trying to create a rewrite rule which will detect numbers only and forward them accordingly. I want the rewrite rule to be ignored if anything but numbers appears.

  • /index.php - OK
  • / - OK
  • /42365 - rewrites to view.php?id=42365

What I have so far:

RewriteEngine on
RewriteRule ^([0-9]+)?$ view.php?id=$1 [L]
like image 281
ATLChris Avatar asked Feb 15 '12 19:02

ATLChris


People also ask

What does rewrite rule do?

htaccess rewrite rule includes setting a combination of rewrite condition ( RewriteCond ) tests along with a corresponding rule ( RewriteRule ) if the prior conditions pass. In most cases, these rules should be placed at any point after the RewriteEngine on line in the . htaccess file located in the website's docroot.

What is rewrite rule in htaccess?

htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.

What is $1 in Apache rewrite rule?

The $1 is basically the captured contents of everything from the start and the end of the string. In other words, $1 = (. *) .


1 Answers

Remove the ? from the end of the ([0-9]+) group, which makes it optional. You must have numbers for the rewrite to occur:

RewriteEngine on
RewriteRule ^([0-9]+)$ view.php?id=$1 [L]
like image 160
Michael Berkowski Avatar answered Oct 02 '22 15:10

Michael Berkowski