How to Mask Extension in URL

后端 未结 4 1635
被撕碎了的回忆
被撕碎了的回忆 2021-01-15 13:35

Is there a way to mask the URL extensions for pages on my website with PHP? Example: http://home/subfolder instead of http://home.subfolder.php

相关标签:
4条回答
  • 2021-01-15 14:06

    Apache httpd's mod_rewrite can rewrite the URL transparently on the server side and let you use any URL you like.

    0 讨论(0)
  • 2021-01-15 14:13

    Yes, it's possible, although it has nothing to do with PHP.

    You'd have to create a file named .htaccess on the root of your webserver (if it does not yet exist). For instance, if you'd want to silently redirect users from /page/software/ to index.php?page=software, you'd use:

    RewriteEngine on 
    RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]
    

    The initial part (^page/([^/\.]+)/?$) is a regular expression. If you're not sure how to use them, give us an example of what you're trying to do, and we can help.

    0 讨论(0)
  • 2021-01-15 14:18

    As others have stated, this is done with mod_rewrite if you're on an Apache-enabled server. Various questions have been asked similar to this:

    Related:

    1. How to hide the .html extension with Apache mod_rewrite
    2. Removing .php with mod_rewrite
    0 讨论(0)
  • 2021-01-15 14:19

    Place the following in the .htaccess file in the root of your website

    <IfModule mod_rewrite.c>
       Options +FollowSymLinks
       Options +Indexes
       RewriteEngine On
       RewriteCond %{SCRIPT_FILENAME} !-d
       RewriteRule ^([^\.]+)$ $1.php [NC,L]
    </IfModule>
    
    0 讨论(0)
提交回复
热议问题