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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化