<p>Is it possible to use .htaccess to process all six digit URLs by sending them to a script, but handle every other invalid URL as an error 404?</p>

<p>For example:</p>

<pre><code>http://mywebsite.com/132483
</code></pre>

<p>would be sent to:</p>

<pre><code>http://mywebsite.com/scriptname.php?no=132483
</code></pre>

<p>but</p>

<pre><code>http://mywebsite.com/132483a or
http://mywebsite.com/asdf
</code></pre>

<p>would be handled as a 404 error.</p>

<p>I presently have this working via a custom PHP 404 script but it's kind of kludgy. Seems to me that .htaccess might be a more elegant solution, but I haven't been able to figure out if it's even possible.</p>

[{'answer_id': 73141, 'author': 'Frank Wiles', 'author_id': 12568, 'author_profile': 'https://Stackoverflow.com/users/12568', 'pm_score': 0, 'selected': False, 'text': "<p>Yes it's possible with mod_rewrite.  There are tons of good mod_rewrite tutorials online a quick Google search should turn up your answer in no time.  </p>\n\n<p>Basically what you're going to want to do is ensure that the regular expression you use is just looking for digits and no other characters and to ensure the length is 6.  Then you'll redirect to scriptname.?no= with the number you captured. </p>\n\n<p>Hope this helps!</p>\n"}, {'answer_id': 73184, 'author': 'Adam Hopkinson', 'author_id': 12280, 'author_profile': 'https://Stackoverflow.com/users/12280', 'pm_score': 4, 'selected': False, 'text': '<p>In your htaccess file, put the following</p>\n\n<pre><code>RewriteEngine On\nRewriteRule ^([0-9]{6})$ /scriptname.php?no=$1 [L]\n</code></pre>\n\n<p>The first line turns the mod_rewrite engine on. The () brackets put the contents into $1 - successive () would populate $2, $3... and so on. The [0-9]{6} says look for a string precisely 6 characters long containing only characters 0-9.</p>\n\n<p>The [L] at the end makes this the last rule - if it applies, rule processing will stop.</p>\n\n<p>Oh, the ^ and $ mark the start and end of the incoming uri.</p>\n\n<p>Hope that helps!</p>\n'}, {'answer_id': 73315, 'author': 'daniels', 'author_id': 9789, 'author_profile': 'https://Stackoverflow.com/users/9789', 'pm_score': 3, 'selected': True, 'text': '<pre><code>&lt;IfModule mod_rewrite.c&gt;\n  RewriteEngine on\n  RewriteRule ^([0-9]{6})$ scriptname.php?no=$1 [L]\n&lt;/IfModule&gt;\n</code></pre>\n\n<p>To preserve the clean URL </p>\n\n<pre><code>http://mywebsite.com/132483\n</code></pre>\n\n<p>while serving scriptname.php use only [L]. \nUsing [R=301] will redirect you to your scriptname.php?no=xxx</p>\n\n<p>You may find this useful <a href="http://www.addedbytes.com/download/mod_rewrite-cheat-sheet-v2/pdf/" rel="nofollow noreferrer">http://www.addedbytes.com/download/mod_rewrite-cheat-sheet-v2/pdf/</a> </p>\n'}]
['https://Stackoverflow.com/questions/73123', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/12513/']