【问题标题】:htaccess seo-friendly URL'shtaccess 对 seo 友好的 URL
【发布时间】:2015-01-13 10:26:01
【问题描述】:

我需要一些帮助才能在我的网站上获得友好的 URL。

这些是我需要重写的链接:

www.example.com/index.php?page=pagename
www.example.com/index.php?page=profile&id=number

这是我的 .htaccess。我解决了第一个链接的问题,但是我不能解决第二个链接的问题。

我想要第二个链接:

www.example.com/profile/name or
www.example.com/profile/id

这是我的 .htaccess 文件:

# for www.example.com/index.php?page=profile&id=number
RewriteCond %{THE_REQUEST} \?page=([^&]+)&id=([0-9]+)
RewriteRule ^ /%1/%2? [L,R=301]

# for www.example.com/index.php?page=page
RewriteCond %{THE_REQUEST} \?page=([^&\ ]+)($|\ )
RewriteRule ^ /%1? [L,R=301]

# rewrites back
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([0-9]+)$ /index.php?page=$1&id=$2 [L]

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

我上面提交的代码解决了我的问题。

【问题讨论】:

    标签: .htaccess mod-rewrite url-rewriting seo friendly-url


    【解决方案1】:

    你可以使用:

    # for www.example.com/index.php?page=profile&id=number
    RewriteCond %{QUERY_STRING} page=([^&]+)&id=([0-9]+)
    RewriteRule ^ /%1/%2? [L,R=301]
    
    # for www.example.com/index.php?page=page
    RewriteCond %{QUERY_STRING} page=([^&]+)
    RewriteRule ^ /%1? [L,R=301]
    

    对于重写

    # rewrites back
    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]
    
    RewriteRule ^([^/]+)/([0-9]+)$ /index.php?page=$1&id=$2 [L]
    RewriteRule ^([^/]+)$ /index.php?page=$1 [L]
    

    【讨论】:

    猜你喜欢
    • 2015-04-17
    • 2011-05-29
    • 2013-01-22
    • 2013-02-03
    • 2011-05-09
    • 1970-01-01
    • 2016-03-12
    • 1970-01-01
    • 2021-07-23
    相关资源
    最近更新 更多