【问题标题】:Rewrite and Redirect urls to change the folder name from the urls重写和重定向 url 以从 url 更改文件夹名称
【发布时间】:2016-05-03 18:45:19
【问题描述】:

问题:

https://mydomain/folder1/xyz/product1

https://mydomain/xyz/product1

记住文件夹 1 不存在。我只想在外部和内部重写它应该指向旧链接。例如

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

尝试过的解决方案 重写引擎开启 重写基础/

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

【问题讨论】:

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


    【解决方案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]
    

    【讨论】:

    • 为什么这个 php 在第 5 行和第 6 行
    • @AnkitJain 对不起,你是什么意思?
    • RewriteRule ^(.+?)/$ $1.php [L] 我没明白这行的意思。为什么你在其中使用.php。我的网址不包含php
    • @Ankitjain 这是将 file 重写为 /file.php RewriteCond 检查 Documentroot/$1.php 是否为文件 b> 然后重写它。没有 .php 的 RewriteCond 无效
    • 对不起,如果我误解了什么,它只是改进了你的原始规则。
    猜你喜欢
    • 1970-01-01
    • 2013-07-18
    • 2015-07-10
    • 1970-01-01
    • 2014-12-07
    • 2014-02-19
    • 1970-01-01
    • 2014-05-15
    • 2021-09-13
    相关资源
    最近更新 更多