.htaccess 不适用于所有链接

发布于 2024-11-04 19:48:30 字数 539 浏览 0 评论 0原文

我的新网站中有两个 URL 示例。

  1. 示例1: http://localhost/phone/3
  2. 示例2:http://localhost/phone/3/10

请注意: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.

  1. sample1: http://localhost/phone/3
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

荒路情人 2024-11-11 19:48:30

使用这个:

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

Use this:

RewriteRule phone/([^/]+)$ /index.php?menu=$1
RewriteRule phone/([^/]+)/([^/]+)$ /index.php?menu=$1&some=$2

Or if your params are always integers:

RewriteRule phone/([0-9]+)$ /index.php?menu=$1
RewriteRule phone/([0-9]+)/([0-9]+)$ /index.php?menu=$1&some=$2
知你几分 2024-11-11 19:48:30

我建议你使用 ([^/]+) 而不是 (.*) ,

区别在于 ([^/]+) 不包含正斜杠。当您的 url 以斜杠结尾时 (.*) 会失败。例如:

home/([^/]+)/?$ index.php?menu=$1 

将完美工作

sitename.com/home/contact

sitename.com/home/contact/

您将得到一个包含两个 url 的联系人的 $_GET['menu'] 。对于 (.*),第二个将包含 contact/,因此后面带有正斜杠。

以 /?$ 结尾表示末尾的 / 是可选的。

编辑:

htaccess 格式的完整代码:

RewriteEngine on 
RewriteRule ^phone/([^/]+)/?$ index.php?menu=$1
RewriteRule ^phone/([^/]+)/([^/]+)/?$ index.php?menu=$1&var2=$2

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:

home/([^/]+)/?$ index.php?menu=$1 

will work perfectly for

sitename.com/home/contact

and

sitename.com/home/contact/

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:

RewriteEngine on 
RewriteRule ^phone/([^/]+)/?$ index.php?menu=$1
RewriteRule ^phone/([^/]+)/([^/]+)/?$ index.php?menu=$1&var2=$2
十六岁半 2024-11-11 19:48:30
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

试试这个

RewriteRule ^(.*)$ /index.php?menu=$1
RewriteRule ^(.*)/(.*)$ /index.php?menu=$1&some=$2

UPDATED Again

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

Try this

葬花如无物 2024-11-11 19:48:30

这应该做你想做的事:

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]

//OR the following if the values are always will be integers (这是最好的方法)

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

我不建议使用 (.*?)。最好具体说明您想要的值是什么。

绝对路径比静态路径更好。

静态路径

<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 更容易。

This should do what you want:

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]

//OR the following if the values are always going to be integers (This is the best way)

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

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

<script src="script.js" type="text/javascript"></script>

Absolute Path

<script src="http://localhost/phone/script.js" type="text/javascript"></script>

If your worried and transportability you could create a settings file, with this in:

define("WWW_DOMAIN", "http://localhost/phone/");

Use this on the top of every page as:

And then do:

<script src="<?php echo WWW_DOMAIN;?>script.js" type="text/javascript"></script>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文