【问题标题】:.htaccess rewrite or subdomain.htaccess 重写或子域
【发布时间】:2015-10-19 12:51:43
【问题描述】:

这是我目前显示的网址;

http://blog.example.co.uk/post?id=152&title=titleofpost

这是我的htaccess,如下图;

外部重定向 /dir/foo.php 到 /dir/foo

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

在内部将 /dir/foo 重定向到 /dir/foo.php

RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]

博客子域

RewriteCond %{HTTP_HOST} blog.example.com$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule ^(.*)$ /blog/$1 [L]

如何将网址显示为;

http://blog.example.co.uk/post/titleofpost

【问题讨论】:

    标签: php regex apache .htaccess


    【解决方案1】:

    像这样:

    Options -MultiViews
    RewriteEngine On
    
    # external redirect from actual URL to pretty one
    RewriteCond %{THE_REQUEST} \s/+post(?:\.php)?\?id=([^\s&]+)&title=([^\s&]+)\s [NC]
    RewriteRule ^ /post/%1/%2? [R=302,L,NE]
    
    # external redirect from actual URL to pretty one
    RewriteCond %{THE_REQUEST} \s/+author(?:\.php)?\?name=([^\s&]+)\s [NC]
    RewriteRule ^ /author/%1? [R=302,L,NE]
    
    # To externally redirect /dir/foo.php to /dir/foo
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
    RewriteRule ^ %1 [L,R=302]
    
    # internal forward from pretty URL to actual one
    RewriteRule ^post/([\w-]+)/([\w-]+)/?$ post.php?id=$1&title=$2 [L,QSA,NC]
    
    # internal forward from pretty URL to actual one
    RewriteRule ^author/([\w-]+)/?$ author.php?name=$1 [L,QSA,NC]
    
    # To internally redirect /dir/foo to /dir/foo.php
    RewriteCond %{REQUEST_FILENAME}.php -f [NC]
    RewriteRule ^ %{REQUEST_URI}.php [L]
    
    # Blog subdomain
    RewriteCond %{HTTP_HOST} ^example\.domain\.com$ [NC]
    RewriteCond %{REQUEST_URI} !^/blog/ [NC]
    RewriteRule ^(.*)$ /blog/$1 [L]
    

    【讨论】:

    • 我需要获得idtitle 我在哪里添加[QSA]
    • 由于某种原因,点击帖子页面上的链接将其添加到 url。例如,指向另一个页面的链接 来自帖子页面的评论将显示为http://blog.example.co.uk/post/id/titleofpost/Review 这是正常的吗?如果我将链接更改为/review.php,那么它可以工作,但这并不理想。我有很多指向其他页面的链接,为什么要将页面名称添加到友好 url?
    • 这是由于您使用了相对链接。您可以尝试将其添加到页面 HTML 的 <head> 部分下方:<base href="/" /> 以避免此问题。
    • 谢谢你是一个救星。有没有办法隐藏id?并且只显示http://blog.example.co.uk/post/titleofpost
    猜你喜欢
    • 2011-05-15
    • 2011-02-04
    • 2014-03-17
    • 1970-01-01
    • 1970-01-01
    • 2010-12-29
    • 2011-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多