1

Using mod_rewrite, I have a series of server-side redirects (Meaning that the client is not made aware of the URI changes.) I want to make it so that any client-side references to index.php return a 404 error without disruption to the server-sided requests...

--

Example..

Client requests x.html, it loads the content of index.php..

Client requests index.php, client gets a 404 Not Found error..

--

I understand that this is poorly formed and could probably be explained a lot better but I'm hoping that this is explained well enough - if it's not, I apologize...

--

I've spent the last three hours on google, trying to find the solution to this and have come up short as I don't really know what it would be understood as...

Wayne
  • 351
  • 1
  • 13

2 Answers2

2

The answer depends on your context : if you are NOT in a .htaccess or in a <Directory>, this is easy :

RewriteEngine On
RewriteRule  /index.php     -           [L,R=404]
RewriteRule  /x.html        /index.php  [L]

If you are in a .htaccess or in a <Directory>, the request is processed a second time, see here. I used an environment variable to work around that :

RewriteEngine On
# after an internal redirect, the variable is prefixed with REDIRECT_
RewriteCond %{ENV:REDIRECT_redirect_to_php} !=true
# no leading / this time
RewriteRule  index.php     -           [L,R=404]
RewriteRule  x.html        /index.php  [L,E=redirect_to_php:true]

You can use RewriteLog and RewriteLogLevel to get the details of the rewrite execution.

David Duponchel
  • 3,959
  • 3
  • 28
  • 36
  • Thanks so much! Edit: I'm in the root of the website and not using any directory tags or anything of the such but the first option is still returning a 404 trying to load index.php – Wayne Apr 05 '15 at 09:52
0

Maybe the [END] tag is what you are searching. As David mentioned, the problem is that the request is processed a second time. As far as I unterstand, you can use [END] to prevent this behavior. See https://stackoverflow.com/a/286005/4751174 (at the end of the answer) or the mod_rewrite doc:

Using the [END] flag terminates not only the current round of rewrite processing (like [L]) but also prevents any subsequent rewrite processing from occurring in per-directory (htaccess) context. http://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_end

Needs Apache 2.3.9 or later.

Community
  • 1
  • 1
codelk
  • 91
  • 1
  • 9
  • I already verified and accepted the previous answer. It is fully functional in the way that I want it to be. – Wayne Apr 06 '15 at 12:22