簡體   English   中英

.htaccess 不適用於所有鏈接

[英].htaccess is not working on all links

我的新站點中有兩個 URL 樣本。

  1. 樣品1: http://localhost/phone/3
  2. 樣品2: http://localhost/phone/3/10

請注意:電話是項目名稱

我自己寫了一個 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM