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

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

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站