apache - Is it better to handle friendly/clean/pretty URLs with mod_rewrite or a language like PHP? -
I am developing my first decent sized PHP site, and I "believe it" To handle clean / friendly / beautiful URLs in the application.
The way I look at it, there are two main options (one example I use a simplified social news site):
1 to handle all possible URLs Use mod_rewrite for it looks similar to the following, but not the same:
rewrite rule ^? ([^ /] *) /? ([^ /] *) /? ([^ /] *) / Content / article Php? Articleid = $ 1 & slug = $ 2 rewrite rule / user (?) ([^ /] *) /? ([^ /] *) / Content / user.fip? User == 1 app; Username = $ 2 Remarket Rule ^ Search / ([^ /] *) /? /content/search.php?query=$1
2. Pass everything in some handler script and let it be worried about the details:
rewrite angle% {REQUEST_FILENAME}! -f rewrite code% {REQUEST_FILENAME}! -D Rivetrium (. *) Handler. FP? Content = $ 1
Obviously this is all unwanted "air code", but you get the point.
- Is one of these two ways slower than the other? Probably mod_rewrite is slower, because I will be forced to use .htaccess files for it.
- Can there be any serious damage to these methods?
- Is there "best practice" for such things, or is it something that every developer decides for himself? I know that WordPress has used option two (though it was more of a hassle when I checked it exactly how it was).
option 1 (.htaccess and many .php files) Often used "in the past"; Now, I have used too many options 2 (each request is going through an .php file)
The main option which I see with option 2:
- You can use any type of file without changing any physical file like .htaccess
- You can add / modify URLs, which means that the format of the URL can be configured in the admin section of your application, for example
- You only have an entry point Your PHP code has
- Which means that everything happens When
index.php
: If you need to execute some code to all requests, so keep it there, and you are sure that it will always be executed.
- Which means that everything happens When
A few years ago, I was with the option 1; Now that I use MVC and Framework, I always go with Option 2.
Comments
Post a Comment