重写和重定向网址以更改网址

时间:2016-05-03 18:45:20

标签: wordpress apache .htaccess mod-rewrite url-rewriting

问题:

https://mydomain/folder1/xyz/product1

https://mydomain/xyz/product1

记住folder1不存在。我只是想在外部和内部重写它应该指向旧的链接。例如

如果用户点击:https://mydomain/folder1/xyz/product1,则用户会自动显示链接https://mydomain/xyz/product1 在浏览器中但内容仍应来自https://mydomain/folder1/xyz/product1

尝试了解决方案     RewriteEngine On     RewriteBase /

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(folder)\ [NC]
RewriteRule ^ /%1/? [R=302,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\-f
RewriteRule ^(.+?)/$ $1.php [L]

.htaccess文件

RewriteOptions inherit

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress


<IfModule mod_rewrite.c>
RewriteEngine on

RewriteBase /
RewriteRule ^best-price/(.+)$ /$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
</IfModule>



# Header unset Pragma
# FileETag None
# Header unset ETag
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##
<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>
# Disable server signature
ServerSignature Off

1 个答案:

答案 0 :(得分:1)

要将/ folder1 / xyz重写为/ xyz,请尝试以下规则:

RewriteEngine on

RewriteRule ^folder1/(.+)$ /$1 [L]

htaccess:

RewriteEngine on

RewriteBase /
RewriteRule ^folder1/(.+)$ /$1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]