doudiyu1639 2017-12-29 18:47
浏览 44

如何重写网站的路径? [重复]

This question already has an answer here:

actually I'm trying to build up a website to get better in coding but no matter how much i am reading, I dont get how to rewrite paths.

Any tipps how to rewrite http://domain/login/php/login.php to http://domain/login.php ?

Best regards, a Saltyy noob :c

</div>
  • 写回答

1条回答 默认 最新

  • douchenzhan3050 2017-12-29 19:05
    关注

    For rewriting the Path you need to include .htaccess file in your root directory to change Apaches configuration setting.

    The .htaccess route with mod_rewrite

    Add a file called .htaccess in your root folder, and see the below details:

    This snippet in your .htaccess will ensure that all requests for files and folders that does not exists will be redirected to index.php:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule . index.php [L]
    

    This enables the rewrite engine:

    RewriteEngine on
    

    This checks for existing folders (-d) and files (-f):

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    

    And this does the actual redirecting:

    RewriteRule . index.php [L]
    

    You can extend this to pass the requested path to the index.php file by modifying the RewriteRule to the following:

    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
    

    The ^(.*)$ part tells the rewrite module that we want to pass down the whole requested path as one parameter. The QSA part tells the module to append any query strings to the request. The ?q=$1 tells the module how to pass down the parameter. In this case, it's passed down as the q parameter. You can extend this even further by using regular expressions. For example:

    RewriteRule ^([^/]*)(.*)$ index.php?first=$1&second=$2
    

    This will pass down the first part of the path as the first parameter, and the rest as the second. So the following request

    http://yourhost.com/some/path/somewhere
    

    will result in

    http://yourhost.com/index.php?first=some&second=path/somewhere
    
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?