从URL中删除子文件夹

时间:2016-10-21 08:52:04

标签: regex apache .htaccess mod-rewrite

我要在URL - 地址中创建漂亮的链接。

例如我有:

mysite.com/language/en/

language删除url文件夹:

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>

1 个答案:

答案 0 :(得分:1)

你的规则如下:

#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>