Reputation: 127
I want .htaccess to return error 404 for any url that does not meet below url pattern criteria. How can I do this in .htaccess?
Using this code in php script generates above url
$adscatcanonurl = "$script_url" . "/" . "{$vbasedir}$xcityid-$xcitynamealt/posts/$xcatid-$catname_inurl/". ("$xsubcatid"?"$xsubcatid-". RemoveBadURLChars($xsubcatname) :"");
Sample link
http://www.domain.com/17-Netherlands/posts/8-Real-Estate/
Let's say script generates a link like this http://www.domain.com/17-Netherlands/posts/8/
As it's not matching with above url pattern, such urls should return 404 page not response.
Upvotes: 1
Views: 558
Reputation: 1151
Why not use: ErrorDocument 404 /404page.html
http://www.yourhtmlsource.com/sitemanagement/custom404error.html
Upvotes: 0
Reputation: 270775
You don't need to force .htaccess to return a 404, it will simply do so on its own if there is no match. So you can use a rule like:
RewriteEngine On
# Don't match real files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(\d+)-([a-zA-Z-]+)/posts/(\d+)-([a-zA-Z-]+)$ your_php_script.php?xcityid=$1&xcitynamealt=$2&xcatid=$3&xcatname_inurl=$4 [L,QSA]
Any request that isn't for an actual file (css, js, etc) and doesn't match the above rule won't match any rule, and should therefore return a 404.
Note that in the rule, I mapped the 4 components to what would be read in your PHP as $_GET['xcityid']
, etc.
Upvotes: 1