PHP URL Format without .php extension?

纵饮孤独 提交于 2020-01-23 02:14:26

问题


all I need is to get a string as URL without showing the PHP filename. I have file contact.php page I am trying to get URL as e.g.

localhost/contact.php

this is what now I am getting I need to access this page just with

localhost/contact

For every page I should able to navigate by their name only.

localhost/contact
localhost/help
localhost/support


回答1:


You need to use an .htaccess file if you are using apache. If you are using IIS then you can configure it in IIS.

Have a look at this.

Your .htaccess file would need to look something like this:

IndexIgnore * # prevent directory listing

Order deny,allow
Allow from *

# ------------------------------------------
# Rewrite so that php extentions are not shown
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

You cannot simply create a .htaccess file in windows because of file constraints, so open a text editor (Notepad ++ or your programming IDE) and create a new file called .htaccess. The . is important in front of the file name.

Edit

Do not have duplicate file names. IE contact.php and contact.js as the htaccess will not know which one to serve, which (depending on your apache) will either return an error page or it will serve one of them only.

As @AedixRhinedale pointed out in the comments below:

Just a note from Apache: You should avoid using .htaccess files completely if you have access to httpd main server config file. Using .htaccess files slows down your Apache http server. Any directive that you can include in a .htaccess file is better set in a Directory block, as it will have the same effect with better performance




回答2:


It's as easy as this..

1) Create .htaccess file in directory (windows has restrictions around this as mentioned above).

2) Just Insert this..

# Turn mod_rewrite on
RewriteEngine On

##(Optional) Redirects http to https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

#### hiding .php extensions below:

# (optional) Ignore logic.php .. any page that is needed as mvc with .php exstensions 
RewriteCond %{THE_REQUEST} ^/logicpath/logic.php [NC]
RewriteCond %{THE_REQUEST} ^/logicpath/funcs.php [NC]
# (optional end ^)

## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]


# (optional) Does the same for html paths
## To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo.html to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.php [L]
# (optional end ^)

As mentioned prior, the httpd main server config file is much better. I am not really sure how to work around the main config files, But I know it's not exatcly the same from small experience.

If someone else knows a good resource on learning how to modify config files please mention!



来源:https://stackoverflow.com/questions/32139982/php-url-format-without-php-extension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!