.htaccess 不适用于所有链接
我的新网站中有两个 URL 示例。
请注意:phone是项目名称
而且我自己写了一个htaccess文件,但是它仅适用于sample1。
Options +FollowSymLinks
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?menu=$1 [L]
我需要一个适用于这两个样本的版本。任何帮助将不胜感激。
I have two URL samples in my new site.
- sample1: http://localhost/phone/3
- sample2: http://localhost/phone/3/10
please note: phone is the project name
And I wrote an htaccess file myself, but it's only working with sample1.
Options +FollowSymLinks
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?menu=$1 [L]
I need a version that works on both samples. Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用这个:
或者如果你的参数始终是整数:
Use this:
Or if your params are always integers:
我建议你使用 ([^/]+) 而不是 (.*) ,
区别在于 ([^/]+) 不包含正斜杠。当您的 url 以斜杠结尾时 (.*) 会失败。例如:
将完美工作
,
您将得到一个包含两个 url 的联系人的 $_GET['menu'] 。对于 (.*),第二个将包含 contact/,因此后面带有正斜杠。
以 /?$ 结尾表示末尾的 / 是可选的。
编辑:
htaccess 格式的完整代码:
I suggest you use ([^/]+) instead of (.*)
The difference is that ([^/]+) doesn't include forward slashes. (.*) fails when you have a url that ends with a slash. For example:
will work perfectly for
and
you'll get a $_GET['menu'] containing contact with both url. With (.*) the second one will contain contact/ so with a trailing forward slash.
ending with /?$ means that the trailing / is optional at the end.
Edit:
complete code in htaccess format:
再次更新
试试这个
UPDATED Again
Try this
这应该做你想做的事:
//OR the following if the values are always will be integers (这是最好的方法)
我不建议使用 (.*?)。最好具体说明您想要的值是什么。
绝对路径比静态路径更好。
静态路径
绝对路径
如果您担心可移植性,您可以创建一个设置文件,其中包含以下内容:
在每个页面的顶部使用此文件:
然后做:
在将网站移动到不同的服务器之前,使网站更容易在本地开发。那么您所要做的就是更改 WWW_DOMAIN 一次。
可能比使用 htaccess 更容易。
This should do what you want:
//OR the following if the values are always going to be integers (This is the best way)
I wouldn't recommended using (.*?). Its best to be specific what you want the value to be.
Absolute paths would be better over static paths.
Static Path
Absolute Path
If your worried and transportability you could create a settings file, with this in:
Use this on the top of every page as:
And then do:
Make a website easier to develop locally before moving it do a different server. Then all you have to do is change WWW_DOMAIN once.
Might be easier than doing it with htaccess.