简体   繁体   中英

How to change url in PHP (Static pages)

I am new to PHP. I am developing a website (as of now with static content, no database involved yet). The url is seen like this for eg : localhost/main/listcontent/content1#abc.php

I want to hide the entire path and replace it with something like : localhost/main?sub=1&brch=1

Any suggestions?

PS : I have googled it but couldn't understand how to proceed.

Thanks in advance !

One way would be with a .htaccess rule:

RewriteEngine on
Options +FollowSymLinks
RewriteBase /

RewriteRule !(\.xpi|\.xml|\.txt|\.air|\.exe|\.zip|\.pdf|\.ico|\.gif|\.jpg|\.png|\.jpeg|\.js|\.swf|\.css|\.php|\.html)$ index_mod_rewrite.php [L]

Inside index_mod_rewrite.php you resolve the current URL to a mapping created by you (see $_SERVER["REQUEST_URI"]), and then require the necessary .php files.

Another would be to manually (or using a script) add all mappings to .htacccess.

The above is valid if you are using Apache. .htaccess is a file you place in any folder, and it affects the current folders and subfolders. Subsequent .htacccess files along the path can alter the configuration or even stop mod rewrite if you so choose for a particular folder.

For the sake of making sure, I am also adding here another requirement for the up most .htaccess file (security feature to disable directory listing):

options All -Indexes
IndexIgnore *

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