简体   繁体   中英

Rewriting page urls in website

I created a simple website and some of the page urls are having the pattern "*.php".

I want to make my url /home.php as /home . Can I do this in .htaccess file?

I am adding my code here

<files campus_config.ini>
order allow,deny
deny from all
Allow from 127.0.0.1
</files>

<files student.ini>
order allow,deny
deny from all
Allow from 127.0.0.1
</files>

<FilesMatch (variables|mysql)\.php>
order allow,deny  
deny from all
</FilesMatch>

RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

sample url: localhost/test/campus/login.php (test is a folder ,campus is my project folder ,.htaccess file is in my project folder)

What you are looking for is called URL rewriting. This is normally done by application server usually Apache or IIS (on windows). how to do it in Apache . On windows it depends on version (6-8) of IIS used.

Place this line on in your /test/campus/.htaccess :

RewriteEngine on
RewriteBase /test/campus/

RewriteCond %{THE_REQUEST} /(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ $1.php [L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM