【问题标题】:How to perfect url in address bar php, .htaccess, or javascript如何完善地址栏 php、.htaccess 或 javascript 中的 url
【发布时间】:2016-01-22 01:20:24
【问题描述】:

这是我的 .htaccess :

<IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine on
    ErrorDocument 404 /?page=404

    # 1-level
    RewriteRule ^contact/{0,1}$  index.php?p=contact [QSA,L]
    RewriteRule ^displaydata/{0,1}$  index.php?p=displaydata [QSA,L]
    RewriteRule ^log-in/{0,1}$  index.php?p=log-in [QSA,L]
    RewriteRule ^log-out/{0,1}$     ?p=log-out [QSA,L]

    # newweb.com/contact# = newweb.com/contact/#
    #RewriteRule ^contact/(.*)?$ $1
    #RewriteRule ^displaydata/(.*)?$ $1
    #RewriteRule ^log-in/(.*)?$ $1
    #RewriteRule ^log-out/(.*)?$ $1


    ####
    # set URI to /index.php/200 if query string is id=200
    RewriteCond %{QUERY_STRING} (?:^|&)id=(200|1)(?:&|$) [NC]
    RewriteRule ^(index\.php)/?$ $1/%1 [NC]

    # set SECURED var to 1 if URI is /index.php/200
    SetEnvIfNoCase Request_URI "^/index\.php/(200|1)" SECURED

    # enforce auth if SECURED=1
    AuthType Basic
    AuthName "Login Required"
    AuthUserFile /full/path/to/passwords
    Require valid-user
    Order allow,deny
    Allow from all
    Deny from env=SECURED
    Satisfy any
    #####
    # all other cases
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .+ -  [L]
    </IfModule>

这是 Javascript:

 var reg_e=/#$/; if(!window.location.href.match(reg_e)) 
    { 
    	window.location.href = decodeURIComponent(window.location.href)+"#";
    } 

地址栏中我的网址:

newweb.com

添加了“#”:

newweb.com/contact#

这真的很好而且有效。但!我的问题是:

我想要这个网址

newweb.com/contact/#

成为

newweb.com/contact#

因为 css 不会使用 / 加载。所以我加了RewriteRule ^contact/(.*)?$ $1

它适用于联系人/#

但是当我点击其他链接时,我会得到:

newweb.com/contact/displaydata#

而不是:

newweb.com/displaydata#

如何完美的url?非常欢迎发表意见和文章。

【问题讨论】:

  • 在你的&lt;head&gt; 部分设置一个&lt;base /&gt; 标签。
  • 嗨。蒙塔希尔·阿肯。我添加了它但没有工作
  • 还是同样的问题?
  • 当有人尝试在最终网址newweb.com/displaydata/# 中添加“/”时,您有什么方法可以返回newweb.com/displaydata# 或不更改页面?
  • 并非如此。在我的网站中,我总是使用&lt;base href="/"&gt;,这样所有的链接都将是根目录的绝对链接。因此,无论用户尝试做什么,都无关紧要。

标签: javascript php .htaccess mod-rewrite


【解决方案1】:

试试这个可能,有时可能会奏效。把它放在你的&lt;head&gt;下面:

<base href="/" />

或者使用srchref,使其与域相关。

<a href="/some/path/to/file"></a>
<link rel="" href="/some/path/to/file" />
<script src="/some/path/to/file"></script>
<img src="/some/path/to/file" />

当您以这种方式使用任何 URL 时,您将能够得到它:

"http://example.com/my/path/#".substr(-2)  // "/#"

因此,保持这一点,您可以这样做:

var url = "http://example.com/my/path/#";
if (url.substr(-2) == "/#")
  window.location.href = url.substr(-2) + "#";

【讨论】:

  • css、popup、login、contact、displaydata同级文件夹是newweb文件夹的子文件夹。 newweb 文件夹中的 index.php,
  • @TríĐứcĐào 你用的是linux机器吗?至少,服务器托管在哪里?
  • 我正在使用窗口。那是本地主机:8080
  • 当有人尝试在最终网址 newweb.com/displaydata/# 中添加“/”时,您有什么方法可以返回 newweb.com/displaydata# 或不更改页面?
  • @TríĐứcĐào 喜欢什么?方法?
猜你喜欢
  • 1970-01-01
  • 2014-06-29
  • 2015-03-06
  • 1970-01-01
  • 2013-12-30
  • 2013-02-15
  • 1970-01-01
  • 1970-01-01
  • 2021-04-06
相关资源
最近更新 更多