开发者

What's the best way to get rid of .php suffix in url strings so they look pretty? [duplicate]

开发者 https://www.devze.com 2023-01-03 15:25 出处:网络
This question already has answers here: 开发者_运维百科Closed 12 years ago. Possible Duplicate: Remove .php extension with PHP
This question already has answers here: 开发者_运维百科 Closed 12 years ago.

Possible Duplicate:

Remove .php extension with PHP

What's the best way to get rid of .php suffix in url strings so they look pretty?

Thank you in advance;-)


Use apache mod_rewrite (rewriting rules)

http://roshanbh.com.np/2008/02/hide-php-url-rewriting-htaccess.html


Make sure your apache installation has mod_rewrite enabled (will be in httpd.conf, or one of the files linked there, mods-enabled or such) and look into how routing works in cakePHP.

Couple of tips - the rewrite rules are found in the .htaccess files (make sure you don't have a unicode BOM if the server gives a 500 error) and if you do find you need those $_GET paramters, [qsappend] on your rewrite rule should pass them along. If you still get 500s the compilation errors on regexes can be found in apache's error log, invaluable for debugging.

Might be easier to do a simple project with mod_rewrite first, to learn how it works, as the combination of rewrite and routing in cake can get pretty complex pretty fast.


Options +MultiViews

in the Apache configuration.


Here is a gentle introduction into mod_rewrite.


The best way to do so (at least for me) is:

  • Use just one file to receive all request. In most of the cases it will be the index.php file.
  • Then, use mod_rewrite rules like this:

:

RewriteEngine On
RewriteBase /the_base_dir_of_your_app/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /the_base_dir_of_your_app/index.php [L]
  • Then, you can analize the URL using functions like basename($_SERVER['REQUEST_URI']); in order to decide what to do.


Use mod_rewrite - or start using ASP.NET MVC 2 :)


If you use a framework, like CakePHP (or any other) it will do it for you. For free. Right now.


.htaccess:


Permalinks

RewriteEngine on

Remove www

RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]

RewriteRule (.*) http://yourdomain.com/$1 [R=301,L]

Links

RewriteRule ^faq$ /faq.php [L]

RewriteRule ^donations$ /donations.php [L]

RewriteRule ^contact$ /contact.php [L]


so they look pretty?

Beauty is in the eye of the beholder. It depends when you consider 'pretty'. A lot also depends on how much you want to get away from the conventions that make a working system possible and the constraints in terms of reconfiguring your site.

While others have mentioned using mod_rewrite, or URL parsing or other such approaches I'm not a fan of these - in addition to being very specific to the type of webserver the code is running on they also break the simple 1:1 mapping beween paths in URIs and paths on the webserver's filesystem.

You could just substitute '.php' with an extension of your choice...but that hardly meets my interpretation of 'pretty'.

The approach I take is to have every script (or at least every script with is intended to be entry point to generaeing a web page) is named as index.php and exists in its own uniquely named directory. The main reason for doing this is nothing to do with making the URL look nice but rather to make the codebase more manageable - I also have strict standards about the naming and placement of include files.

HTH

C.

0

精彩评论

暂无评论...
验证码 换一张
取 消