2016-10-21 150 views
1

我要在URL地址中創建漂亮的鏈接地址。從URL中刪除子文件夾

比如我有:

mysite.com/language/en/ 

而且從url刪除language文件夾:

mysite.com/en/ 

.htaccess寫道:

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteRule ^language/(.*)$ /$1 [L,R] 
</IfModule> 

它工作正常,但mysite.com/en/應該有mysite.com/language/en/的內容。現在我有404錯誤。

我該怎麼辦?


更新(全.htaccess):

Options All -ExecCGI -Indexes -Includes +FollowSymLinks 

# Bad Request 
ErrorDocument 400 /error/400.html 

# Authorization Required 
ErrorDocument 401 /error/401.html 

# Forbidden 
ErrorDocument 403 /error/403.html 

# Not found 
ErrorDocument 404 /error/404.html 

# Method Not Allowed 
ErrorDocument 405 /error/405.html 

# Request Timed Out 
ErrorDocument 408 /error/408.html 

# Request URI Too Long 
ErrorDocument 414 /error/414.html 

# Internal Server Error 
ErrorDocument 500 /error/500.html 

# Not Implemented 
ErrorDocument 501 /error/501.html 

# Bad Gateway 
ErrorDocument 502 /error/502.html 

# Service Unavailable 
ErrorDocument 503 /error/503.html 

# Gateway Timeout 
ErrorDocument 504 /error/504.html 

#Rewrite links 
<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteRule ^language/(.*)$ /$1 [L,R] 
</IfModule> 

<ifModule mod_deflate.c> 
    AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/css text/javascript application/javascript application/x-javascript 
</ifModule> 

<ifModule mod_headers.c> 
    <FilesMatch "\.(html|htm)$"> 
     Header set Cache-Control "max-age=43200, public" 
    </FilesMatch> 
    <FilesMatch "\.(js|css|txt)$"> 
     Header set Cache-Control "max-age=2592000, public" 
    </FilesMatch> 
    <FilesMatch "\.(flv|swf|ico|gif|jpg|jpeg|png)$"> 
     Header set Cache-Control "max-age=2592000, public" 
    </FilesMatch> 
    <FilesMatch "\.(pl|php|cgi|spl|scgi|fcgi)$"> 
     Header unset Cache-Control 
    </FilesMatch> 
</IfModule> 
+0

我相信您應該切換'RewriteRule':(。*)'^重寫規則$ /語言/ $ 1 [L,R]' –

+0

@BogdanKuštan之後,我得到了'mysite.com/language * 1000/en')) –

+0

我有規則緩存內容並壓縮它。 .htaccess是單一的。 –

回答

2

讓你的規則是這樣的:

#Rewrite links 
<IfModule mod_rewrite.c> 
Options +FollowSymLinks 
RewriteEngine On 

RewriteCond %{THE_REQUEST} \s/+language/(\S*)\sHTTP [NC] 
RewriteRule^/%1 [R=301,NE,L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(?!language/)(.+)$ language/$1 [L,NC] 
</IfModule> 
+0

謝謝。它的工作原理,但我還有一個問題:我怎麼能每次從'url'拒絕'/ language /'。 –

+0

1st redirect rule will remove'/ language /'from URL – anubhava

+1

是的,我忘了重置瀏覽器緩存 –

相關問題