[英].htaccess is not working on all links
我的新站点中有两个 URL 样本。
请注意:电话是项目名称
我自己写了一个 htaccess 文件,但它只适用于 sample1。
Options +FollowSymLinks
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?menu=$1 [L]
我需要一个适用于两个样本的版本。 任何帮助,将不胜感激。
用这个:
RewriteRule phone/([^/]+)$ /index.php?menu=$1
RewriteRule phone/([^/]+)/([^/]+)$ /index.php?menu=$1&some=$2
或者,如果您的参数始终是整数:
RewriteRule phone/([0-9]+)$ /index.php?menu=$1
RewriteRule phone/([0-9]+)/([0-9]+)$ /index.php?menu=$1&some=$2
我建议你使用 ([^/]+) 而不是 (.*)
不同之处在于 ([^/]+) 不包括正斜杠。 (.*) 当您有一个以斜杠结尾的 url 时失败。 例如:
home/([^/]+)/?$ index.php?menu=$1
将完美地工作
sitename.com/home/contact
和
sitename.com/home/contact/
你会得到一个 $_GET['menu'] 包含与 url 的联系。 使用 (.*) 后,第二个将包含contact/,因此带有斜杠。
以 /?$ 结尾意味着结尾 / 是可选的。
编辑:
htaccess 格式的完整代码:
RewriteEngine on
RewriteRule ^phone/([^/]+)/?$ index.php?menu=$1
RewriteRule ^phone/([^/]+)/([^/]+)/?$ index.php?menu=$1&var2=$2
RewriteRule ^(.*)$ /index.php?menu=$1
RewriteRule ^(.*)/(.*)$ /index.php?menu=$1&some=$2
再次更新
Options +FollowSymLinks
RewriteBase /phone
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f # Do not rewrite static files
RewriteCond %{REQUEST_FILENAME} !-d # Do not rewrite static directories
RewriteRule ^(.*)$ /index.php?menu=$1
RewriteRule ^(.*)/(.*)$ /index.php?menu=$1&some=$2
尝试这个
这应该做你想要的:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /phone/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)/?$ index.php?menu=$1 [L]
RewriteRule ^(.*?)/(.*?)/?$ index.php?menu=$1&submenu=$2 [L]
//或者如果值总是整数,则如下(这是最好的方法)
Options +FollowSymLinks
RewriteEngine On
RewriteBase /phone/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ index.php?menu=$1 [L]
RewriteRule ^([0-9]+)/([0-9]+)/?$ index.php?menu=$1&submenu=$2 [L]
RewriteRule ^(.*?)/(.*?\.js)$ /phone/js/$2 [L] #Makes JS static content work
RewriteRule ^(.*?)/(.*?\.css)$ /phone/css/$2 [L] #Makes CSS static content work
我不建议使用 (.*?)。 最好具体说明您想要的价值。
绝对路径会比 static 路径更好。
Static 路径
<script src="script.js" type="text/javascript"></script>
绝对路径
<script src="http://localhost/phone/script.js" type="text/javascript"></script>
如果您担心和可移植性,您可以创建一个设置文件,其中包含:
define("WWW_DOMAIN", "http://localhost/phone/");
在每一页的顶部使用它作为:
然后做:
<script src="<?php echo WWW_DOMAIN;?>script.js" type="text/javascript"></script>
在将网站移动到不同的服务器之前,让网站更容易在本地开发。 然后您所要做的就是更改一次 WWW_DOMAIN。
可能比使用 htaccess 更容易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.