Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens first? .htaccess or php code?

If I use mod_rewrite to control all my 301 redirects, does this happen before my page is served? so if I also have a bunch of redirect rules in a php script that runs on my page, will the .htaccess kick in first?

like image 768
Chris Avatar asked Oct 01 '08 16:10

Chris


People also ask

Does htaccess work on PHP?

You can use the . htaccess file to modify various configurations and thus make changes to your website. These changes include authorization, error handling, redirects for specific URLs, user permissions, etc.

Where do I put .htaccess file in PHP?

htaccess file in your terminal, you need to navigate to your web root directory. Your web root directory is where to place the . htaccess file so that your configurations can be properly executed for your website.

What is the purpose of using the .htaccess file?

. htaccess files (or "distributed configuration files") provide a way to make configuration changes on a per-directory basis. A file, containing one or more configuration directives, is placed in a particular document directory, and the directives apply to that directory, and all subdirectories thereof.

How do I start PHP code?

If you want to run it, open any web browser and enter “localhost/demo. php” and press enter. Your program will run.


2 Answers

The .htaccess will kick in first. If you look at the Apache request cycle:

Apache Request Cycle

PHP is a response handler. mod_rewrite runs at URI translation, except for rewrite rules in .htaccess and <Directory> or <Location> blocks which run in the fixup phase. This is because Apache doesn't know which directory it's in (and thus which <Directory> or .htaccess to read) until after URI translation.

In response to to gabriel1836's question about the image, I grabbed it from the second slide of this presentation but it's originally from the book: Writing Apache Modules in Perl and C which I highly recommend.

like image 195
bmdhacks Avatar answered Oct 09 '22 22:10

bmdhacks


When a request is made to the URI affected by the .htaccess file, then Apache will handle any rewrite rules before any of your PHP code executes.

like image 44
Noah Goodrich Avatar answered Oct 09 '22 22:10

Noah Goodrich