简体   繁体   English

.htaccess 不适用于所有链接

[英].htaccess is not working on all links

I have two URL samples in my new site.我的新站点中有两个 URL 样本。

  1. sample1: http://localhost/phone/3样品1: http://localhost/phone/3
  2. sample2: http://localhost/phone/3/10样品2: 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.我自己写了一个 htaccess 文件,但它只适用于 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.任何帮助,将不胜感激。

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

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. (.*) 当您有一个以斜杠结尾的 url 时失败。 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.你会得到一个 $_GET['menu'] 包含与 url 的联系。 With (.*) the second one will contain contact/ so with a trailing forward slash.使用 (.*) 后,第二个将包含contact/,因此带有斜杠。

ending with /?$ means that the trailing / is optional at the end.以 /?$ 结尾意味着结尾 / 是可选的。

Edit:编辑:

complete code in htaccess format: 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

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尝试这个

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 路径更好。

Static Path Static 路径

<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.然后您所要做的就是更改一次 WWW_DOMAIN。

Might be easier than doing it with htaccess.可能比使用 htaccess 更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM