【问题标题】:Problems with URL Encoded Characters that don't get rewrited不被重写的 URL 编码字符的问题
【发布时间】:2017-01-01 18:26:53
【问题描述】:

我制定了一些规则,例如重定向匹配,将旧网址动态指向新位置, 但是,每当我在任何 URL 中遇到 URL 编码字符时,重定向根本不起作用。 以下是这些网址的示例:

www.example.com/ebc-drama-yebet-sira/yebet-sira-%E1%8B%A8%E1%89%A4%E1%89%B5-%E1%88%B5%E1%88%AB-drama-part-1-video_80c33bc56.html

所以我需要删除以下字符: %E1%8B%A8%E1%89%A4%E1%89%B5-%E1%88%B5%E1%88%AB
上面的那些字符等于这个阿姆哈拉语单词:የቤት-ስራ
我需要的是一个规则,从 url 中删除非字母数字字符,但保留所有特殊字符,如“-”“_”等只是 URL 编码字符。
我试过了:

RewriteRule ^/?(.*)>$ /$1 [L,R=301]

和:

RewriteRule (.*)[^a-zA-Z0-9](.*) $1$2 [N]

Htaccess 文件:

Options -MultiViews
RewriteEngine On
RewriteRule ^index.html$ index.php
RewriteRule ^browse.html$ category.php
RewriteRule ^browse-(.*)-videos.html$ category.php?cat=$1
RewriteRule ^browse-(.*)-videos-([0-9]+)-(.*).html$ category.php?cat=$1&page=$2&sortby=$3
RewriteRule ^videos.flv(.*)$ videos.php$1
RewriteRule ^videos.mp4(.*)$ videos.php$1
RewriteRule ^register.html$ register.php
RewriteRule ^contact_us.html$ contact_us.php
RewriteRule ^edit_profile.html$ edit_profile.php
RewriteRule ^suggest.html$ suggest.php
RewriteRule ^upload.html$ upload.php
RewriteRule ^upload_avatar.html$ upload_avatar.php
RewriteRule ^suggest.html$ suggest.php
RewriteRule ^favorites.html(.*)$ favorites.php$1
RewriteRule ^login.html(.*)$ login.php$1
RewriteRule ^newvideos.html(.*)$ newvideos.php$1
RewriteRule ^topvideos.html(.*)$ topvideos.php$1
RewriteRule ^profile.html(.*)$ profile.php$1
RewriteRule ^user/([^/]+)/?$ user.php?u=$1
RewriteRule ^user/([^/]+)/(.*)/?$ user.php?u=$1&view=$2
RewriteRule ^memberlist.html(.*)$ memberlist.php$1
RewriteRule ^playlists.html(.*)$ playlists.php$1
RewriteRule ^index-([0-9]+).html$ article.php?page=$1
RewriteRule ^browse-(.*)-([0-9]+).html$ article.php?c=$1&page=$2
RewriteRule ^articles/tag/([^/]+)/page-([0-9]+)(/)?$ article.php?tag=$1&page=$2
RewriteRule ^articles/tag/([^/]+)(/)?$ article.php?tag=$1&page=1
RewriteRule ^popular-([0-9]+).html$ article.php?show=popular&page=$1
RewriteRule ^(.*)_([0-9]+).html$ article_read.php?a=$2
RewriteRule ^articles(\/|.html)?$ article.php
RewriteRule ^article(\/|.html)?$ article.php
RewriteRule ^pages/(.*)\.html$ page.php?name=$1
RewriteRule ^playlist/(.*)/([^/]+)(/)?$ watch.php?playlist=$1&vid=$2
RewriteRule ^playlist/(.*)$ playlists.php?playlist=$1
RewriteRule ^tags/([^/]+)/$ tag.php?t=$1&page=1
RewriteRule ^tags/([^/]+)/page-([0-9]+)(/)?$ tag.php?t=$1&page=$2
RewriteRule ^embed/([^/]+)$ embed.php?vid=$1
RewriteRule ^([^/]*)_([a-zA-Z0-9]{9}).html$ watch.php?vid=$2
RewriteRule ^fpembed-(.*).swf$ fpembed.php?vid=$1
RewriteRule ^uploads/thumbs/(.*)-social.(jpg|gif|png)$ social-thumb.php?vid=$1
RewriteRule ^rss.xml$ rss.php [L]
RedirectMatch 301 ^/[\w-]+/([\w-]+\.html)$ /$1

谢谢。

【问题讨论】:

    标签: php ajax apache .htaccess url-rewriting


    【解决方案1】:

    您可以在站点根目录 .htaccess 中使用这些规则:

    RewriteEngine On
    
    # skip all css/js/images from rewrite rules below
    RewriteRule \.(?:jpe?g|gif|bmp|png|ico|tiff|css|js)$ - [L,NC]
    
    RewriteCond %{REQUEST_URI} !^/(admin|articles?|user|tags|embed|playlist|uploads)(/.*)?$ [NC]
    RewriteRule ^(.*)[^\w/.-]+(.*\.html)$ $1$2 [DPI,N,E=SP:1]
    
    RewriteCond %{ENV:SP} =1
    RewriteRule ^[\w.-]*$ /$0 [L,R=302,NE]
    
    # comment out RedirectMatch 301 rule
    
    # remaining RewriteRule appear here
    

    这将通过删除所有特殊字符将/ebc-drama-yebet-sira/yebet-sira-%E1%8B%A8%E1%89%A4%E1%89%B5-%E1%88%B5%E1%88%AB-drama-part-1-video_80c33bc56.html 重定向到/ebc-drama-yebet-sirayebet-sira---drama-part-1-video_80c33bc56.html

    【讨论】:

    • 抱歉延迟@anubhava,实际上这段代码导致css崩溃或网站无限加载..我必须注意我使用你给我的另一个规则是:RedirectMatch 301 ^/[\w-]+/([\w-]+\.html)$ /$1也是这个旧规则产生了另一个问题,即使指向其他域的子目录中的其他 htaccess 文件不起作用(删除它会解决这个问题,但它在主域中正常工作并且规则很重要)
    • 完成了。请查看更新后的问题。
    • 注释掉 RedirectMatch 301 规则并将我给定的规则放在 RewriteEngine On 行的下方
    • 我按照你说的做了兄弟,我认为影响 htaccess 中其他目录的问题很小,特别是文章相关规则请查看 htaccess 以了解此新规则会影响什么以及如何修理它。再次感谢您出色的工作,非常感谢。
    • 非常感谢,干得好!
    【解决方案2】:

    试试

    [^a-zA-Z0-9-_./]
    

    你可以在这里测试你的规则 http://www.regextester.com/

    【讨论】:

    • 如果我添加 % 那么 url 将保留 % 字符并且永远不会重定向。 martinmelin.se/rewrite-rule-tester
    • 你是对的......我在上面编辑了我的答案..这将删除所有 % 但保留 - 和 _ 以及 /
    • 内部服务器错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多