drplww2505 2015-12-02 18:34
浏览 90
已采纳

在HTACCESS重写并使用$ _SERVER ['PHP_SELF']后,页面源仍显示原始页面名称

I am using an htaccess rewrite to change the name of a page named cities.php to the linked city page when clicked. For example if the link of Aliso Viejo was clicked you would see:

aliso-viejo-slab-leak-detection.php

The rewrite section of my htaccess is as follows:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{HTTP_HOST} !^$
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTPS}s ^on(s)|
    RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301]
    RewriteRule ^([a-z]+)-slab-leak-detection.php$ cities.php?city=$1&state=$2
    RewriteRule ^([a-z-]+)-slab-leak-detection.php$ cities.php?city=$1&state=$2
    RewriteRule ^(.*)\.html$ $1.php [L]
    ErrorDocument 404 /index.php

Now the rewrite works as intended. The problem is for the canonical link I am using:

    $_SERVER['PHP_SELF']

to echo the page url for indexing purposes. Instead of echoing the rewritten page name it is echoing the original cities.php name. Search engines are also indexing the pre rewritten page as well.

I am assuming I have the code in the wrong order or do not have it coded correctly to write silently instead of dynamically.

Bear in mind I am pretty unfamiliar with advanced httaccess coding and the code I have used is from changed information I have researched.

Any help is greatly appreciated.

  • 写回答

1条回答 默认 最新

  • doujianguang5506 2015-12-02 18:54
    关注

    I don't think the value of $_SERVER['PHP_SELF'] is affected by .htaccess rewrites, because it references the filesystem, not the URL.

    If you need to reference the URL, use $_SERVER['REQUEST_URI']

    EDIT

    Just found this related question/answer: Can PHP access the .htaccess rewrite name of a file. Regilero's comment on the selected answer makes a good point about escaping the value of $_SERVER['REQUEST_URI'] before outputting it:

    Beware that you should escape $_SERVER['REQUEST_URI'] before using to build HTML content, avoid users injection HTML/js in the url. Check stackoverflow.com/a/326331/550618 for details on SCRIPT_NAME vs REQUEST_URI

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么