【发布时间】: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?非常欢迎发表意见和文章。
【问题讨论】:
-
在你的
<head>部分设置一个<base />标签。 -
嗨。蒙塔希尔·阿肯。我添加了它但没有工作
-
还是同样的问题?
-
当有人尝试在最终网址newweb.com/displaydata/# 中添加“/”时,您有什么方法可以返回newweb.com/displaydata# 或不更改页面?
-
并非如此。在我的网站中,我总是使用
<base href="/">,这样所有的链接都将是根目录的绝对链接。因此,无论用户尝试做什么,都无关紧要。
标签: javascript php .htaccess mod-rewrite