Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7624353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:56:25+00:00 2026-05-31T04:56:25+00:00

I have a php website and I’m using mod_rewrite to redirect all of the

  • 0

I have a php website and I’m using mod_rewrite to redirect all of the URL requests. Suppose I have a product.php script and I have a rule for it:

RewriteRule ^product/([0-9]+).*/?$ product.php?pid=$1 [L,NC]
RewriteRule ^category/([0-9]+).*/?$ category.php?cid=$1 [L,NC]

So far, this works great and when the user types: http://www.mysite.com/product/1 the page with the actual URL of http:/www.mysite.com/product.php?pid=1 will be shown to him.

However, I want to force the user to access my site, exclusively from the rules that I have provided. By that, I mean when the user types http:/www.mysite.com/something or even some correct URL like http:/www.mysite.com/product.php?pid=1 he should be redirected to the 404 error page. This can be a custom script or apache’s default 404 page.

Is that possible?

EDIT: As requested, I post my entire .htaccess file:

Options +FollowSymLinks
Options +Indexes

RewriteEngine on    
RewriteBase /websitename
RewriteRule ^product/([0-9]+).*/?$ product.php?pid=$1 [L,NC] 
RewriteRule ^category/([0-9]+).*/?$ category.php?cid=$1 [L,NC] 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:56:27+00:00Added an answer on May 31, 2026 at 4:56 am

    You could just add a catch all as the last possible rewrite and map it to 404.php (or whatever you wan’t).

    RewriteRule ^(.*)$ 404.php?attempt=$1 [L,NC]
    

    Just place this in the bottom of your rewrites.

    EDIT:

    I tested this out myself (i usually don’t use rewrites this way):

    RewriteEngine on    
    
    RewriteRule ^product/([0-9]+).*/?$ product.php?pid=$1 [L] 
    RewriteRule ^category/([0-9]+).*/?$ category.php?cid=$1 [L]
    
    RewriteRule ^(.*)$ 404.php?attempt=$1 [L]
    

    And also found that the 404 would be hit everytime. Apparently this is expected behaviour of .htaccess. (https://stackoverflow.com/a/3642271/603184)

    So turns out you need it to stop matching when it hits product/category.php, this can be accomplished via adding:

    RewriteRule ^category.php$ - [L]
    RewriteRule ^product.php$ - [L]
    

    Before the 404 redirect. Resulting in:

    RewriteEngine on    
    
    RewriteRule ^product/([0-9]+).*/?$ product.php?pid=$1 [L] 
    RewriteRule ^category/([0-9]+).*/?$ category.php?cid=$1 [L]
    
    RewriteRule ^category.php$ - [L]
    RewriteRule ^product.php$ - [L]
    
    RewriteRule ^(.*)$ 404.php?attempt=$1 [L]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a PHP contact form on my website. I have been using it
So I have this PHP website that I am working on that using templates.
All, I have a PHP website written in Zend Framework and MVC. Most of
I have a php based website. As in, all of the pages html is
I have a php classifieds website (mostly) and I am currently using MYSQL as
I have a PHP website with a custom page routing system. A php script
I have PHP scrapping script which fetches HTML table content from another website. The
I have an php website with login form and I want every visitors can
I have inherited an big existing PHP application (website actually) that runs on Apache.
I have a php page inside a folder on my website. I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.