2016-02-08 43 views
2

我遇到一些與我的.htaccess文件存在問題,存儲在我的網站的根級別。.htaccess將不會正確重寫

我要重寫以下

  1. mysite.com執行mysite.com/index.php?lang=en但重寫URL到mysite.com/en/
  2. mysite.com/en (沒有結尾的斜線)不與上述相同的
  3. mysite.com/en/blog/page執行mysite.com/blog/page?lang=en

概括地說,該語言添加到URL,打開index.php與.com,.com/en或.com/en /,a第二剝離文件擴展名

下面是引用完整的文件:

RewriteEngine on 

# Redirect to domain without www. 
RewriteCond %{HTTPS} off 
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] 
RewriteRule .* http://%1%{REQUEST_URI} [R=301,L] 
# Same for HTTPS: 
RewriteCond %{HTTPS} on 
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] 
RewriteRule .* https://%1%{REQUEST_URI} [R=301,L] 

# Stop hotlinking. 
RewriteCond %{HTTP_REFERER} !^$ 
RewriteCond %{HTTP_REFERER} ^https?://([^/]+)/ [NC] 
RewriteCond %1#%{HTTP_HOST} !^(.+)#\1$ 
RewriteRule \.(jpg|jpeg|png|gif|svg)$ - [NC,F,L] 

# Prevent directory listings 
Options All -Indexes 

# Request language from URL 
# empty url -> redirect to en/ 
RewriteCond %{QUERY_STRING} !lang=(en|de) 
RewriteRule ^$ en/ [R=301,L] 

# now all urls have en/ de/ -> parse them 
RewriteRule ^(en|de)/(.*)$ $2?lang=$1&%{query_STRING} [L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^\.]+)$ $1.php [NC,L] 

完全公開,它已經全部採取了從各種谷歌的結果,我很欣賞它,也許可以做得更好。

因爲它的立場,mysite.com給我

請求的URL /home/towrafvk/public_html/en/.php此服務器上找到。

mysite.com/en給我

請求的URL /en.php此服務器上找到。

mysite.com/en/並按預期

我知道這個問題是mysite.com/en/blog/page都工作在第三到最後一個和第二個到最後重寫規則。我怎樣才能解決這個問題?

回答

1
RewriteEngine on 

# Redirect to domain http(s) without www. 
RewriteCond %{HTTP_HOST}#%{HTTPS}s ^www\.([^#]+)#(?:off|on(s)) [NC] 
RewriteRule^http%2://%1%{REQUEST_URI} [NE,L,R=301] 

# Stop hotlinking. 
RewriteCond %{HTTP_REFERER} !^$ 
RewriteCond %{HTTP_REFERER} ^https?://([^/]+)/ [NC] 
RewriteCond %1#%{HTTP_HOST} !^(.+)#\1$ 
RewriteRule \.(jpg|jpeg|png|gif|svg)$ - [NC,F,L] 

# Prevent directory listings 
Options All -Indexes 

# Request language from URL 
# empty url -> redirect to en/ 
RewriteCond %{QUERY_STRING} !lang=(en|de) 
RewriteRule ^$ en/ [R=301,L] 

# now all urls have en/ de/ -> parse them 
RewriteRule ^(en|de)/(.*)$ $2?lang=$1 [L,QSA] 

# Use .php file only if not exist without and exist with .php 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f 
RewriteRule ^(.+)/?$ $1.php [L]