我正在努力處理我的.htaccess檔案并按照我想要的方式進行設定。主要功能是從子域獲取語言和從子檔案夾獲取當前頁面的網站。
要求
我需要我的.htaccess檔案來完成三個要求;
- 通配符子域重定向到lang變數
- 子檔案夾重定向到頁面變數
- 尊重本地檔案(這是我卡住的地方)
- (獎勵)將頁面變數拆分為每個斜線的段;頁面、sub1、sub2 等
例子
en.example.com/hello->/index.php?lang=en&page=helloes.example.com/hola->/index.php?lang=es&page=hola- (獎金)
en.example.com/hello/there/sir->index.php?lang=en&page=hello&sub1=there&sub2=sir
我當前的 .htaccess
這是我當前的設定,如果我不需要任何本地檔案(大聲笑),它實際上有點作業。這意味著當 my 處于活動狀態時找不到本地影像.htaccess below。我嘗試添加RewriteCond %{REQUEST_FILENAME} !-f以尊重本地檔案,但這似乎破壞了整個檔案 - 我不知道為什么。
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP_HOST} ((?!www). )\.example\.com [NC]
RewriteRule ^$ /index.php?lang=%1 [L]
RewriteCond %{HTTP_HOST} ((?!www). )\.example\.com [NC]
RewriteRule ^(. )$ /index.php?lang=%1&page=$1 [L]
RewriteRule ^index\.php$ - [L]
RewriteRule ^(.*)$ /index.php?page=$1 [L,QSA]
uj5u.com熱心網友回復:
如果您的 URL 不包含點,則從您的正則運算式中排除點 - 這自然會排除真實檔案(在檔案擴展名前包含一個點)。這避免了對檔案系統檢查的需要。
你的腳本應該處理/index.php?lang=%1和/index.php?lang=%1&page=完全相同,所以第一條規則是多余的。
RewriteRule ^index\.php$ - [L]
這條規則應該放在第一位,而不是嵌入中間。
請嘗試以下操作:
RewriteRule ^index\.php$ - [L]
RewriteCond %{HTTP_HOST} ^((?!www). )\.example\.com [NC]
RewriteRule ^([^.]*)$ /index.php?lang=%1&page=$1 [QSA,L]
RewriteRule ^([^.]*)$ /index.php?page=$1 [QSA,L]
您將其他所有內容重寫為 的最后一條規則index.php,減去langURL 引數是有問題的。為什么不把它包含在前面的規則中并驗證腳本中的語言?無論如何你都需要這樣做。
假設總是有一個子域,那么您的規則可以簡化為:
RewriteRule ^index\.php$ - [L]
RewriteCond %{HTTP_HOST} ^(. )\.example\.com [NC]
RewriteRule ^([^.]*)$ /index.php?lang=%1&page=$1 [QSA,L]
www 然后,您的腳本會驗證對語言的請求并相應地默認設定,就好像lang根本沒有傳遞引數一樣(無論如何您都需要這樣做)。
如果您的子域完全是可選的,并且您正在訪問域頂點,則在正則運算式中將其設為可選(帶有非捕獲組):
RewriteCond %{HTTP_HOST} ^(?:(. )\.)?example\.com [NC]
:
如果請求域頂點,則lang引數將為空。
(獎金)
en.domain.com/hello/there/sir->index.php?lang=en&page=hello&sub1=there&sub2=sir
在您的 PHP 腳本中執行此操作會更好(更有效、更靈活等),而不是.htaccess.
但是.htaccess你可以做這樣的事情(而不是現有的規則):
:
RewriteRule ^([^/.]*)(?:/([^/.] ))?(?:/([^/.] ))?(?:/([^/.] ))?(?:/([^/.] ))?$ /index.php?lang=%1&page=$1&sub1=$2&sub2=$3&sub3=$4&sub4=$5 [QSA,L]
當該路徑段不存在時,URL 引數為空。
It is assumed the URL-path does not end in a slash (the above will not match if it does, so a 404 will result). If a trailing slash needs to be permitted then this should be implemented as a canonical redirect to remove the trailing slash. Or reverse the logic to enforce a trailing slash.
This particular example allows up to 4 additional "sub" path segments, eg. hello/1/2/3/4. You can extend this method to allow up to 8 (since there is a limit of 9 backreferences in the Apache syntax) if required. Any more and you will need to use PHP. (You could potentially handle more using .htaccess, but it will get very messy as you will need to employ additional conditions to capture subsequent path segments.)
I tried adding
RewriteCond %{REQUEST_FILENAME} !-fto respect local files but that breaks the whole file it seems
That should also be sufficient (if dots are permitted in your URLs). But I wonder where you were putting it? It should not "break" anything - it simply prevents the rule from being processed if the request does map to a file - the rule is "ignored".
This is of course assuming you are correctly linking to your resources/static assets using root-relative (starting with a slash) or absolute (starting with scheme hostname) URLs. If you are using relative URLs then they will probably result in 404s. If this is the case then see my answer to the following question on the Webmasters stack:
- https://webmasters.stackexchange.com/questions/86450/htaccess-rewrite-url-leads-to-missing-css
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/436571.html
