【问题标题】:parametric subdomains in url rewritingurl重写中的参数子域
【发布时间】:2014-01-31 19:24:09
【问题描述】:

我有一个漂亮的网址

例如:www.mydomain.co.in 到 subdomain.mydomain.co.in,效果很好..

我面临的问题是参数...... 我得到subdomain.mydomain.co.in/category?id=250..这必须更改为subdomain.mydomain.co.in/category/250,仅使用.htaccess..

是的,有许多链接适用于 www.mydomain.co.in?id=250 到 www.mydomain.co.in/250..

必须进行哪些更改才能包含子域。输出应该类似于subdomain.mydomain.co.in/category/250

.htaccess 文件如下所示

 RewriteEngine On

 #subdomain and folders
 RewriteCond %{HTTP_HOST} !^subdomain\.mydomain.co.in [NC]

 RewriteRule ^(.*)$ http://subdomain.mydomain.co.in/$1 [R=301,L]

 #remove .php and ad slash
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^([^/]+)/$ http://mydomain.co.in/subdomain/$1 [R=301,L]

 # Redirect external .php requests to extensionless url
 RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
 RewriteRule ^(.+)\.php$ http://mydomain.co.in/subdomain/$1 [R=301,L]

 # Resolve .php file for extensionless php urls
 RewriteRule ^([^/.]+)$ $1.php [L]

【问题讨论】:

    标签: php .htaccess mod-rewrite subdomain


    【解决方案1】:

    您需要 2 条新规则来处理 category。试试这个代码:

     RewriteEngine On
    
     #subdomain and folders
     RewriteCond %{HTTP_HOST} !^subdomain\.mydomain.co.in [NC]
     RewriteRule ^(.*)$ http://subdomain.mydomain.co.in/$1 [R=301,L]
    
     #remove .php and ad slash
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteRule ^([^/]+)/$ http://mydomain.co.in/subdomain/$1 [R=301,L]
    
     RewriteCond %{THE_REQUEST} \s/+(category)\?id=([^\s&]+) [NC]
     RewriteRule ^ /%1/%2? [R=302,L]
    
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteRule ^(category)/([^/.]+)/?$ /$1?id=$2 [L,QSA,NC]
    
     # Redirect external .php requests to extensionless url
     RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
     RewriteRule ^(.+)\.php$ http://mydomain.co.in/subdomain/$1 [R=301,L]
    
     # Resolve .php file for extensionless php urls
     RewriteRule ^([^/.]+)$ $1.php [L]
    

    【讨论】:

    • 这可能是由于 css/js 未加载。尝试在您的页面标题中添加此内容:<base href="/" /> 或者在您的 css、js、图像文件中使用绝对路径,而不是相对路径。这意味着您必须确保这些文件的路径以 http:// 或斜杠 / 开头。
    • ,谷歌搜索,但没有得到这背后的逻辑..你能解释一下category部分背后的逻辑吗?
    • 哦,当然。第一条规则是 301 规则,从外部重定向从 /category?id=250/category/250,而下一条规则用于内部重写,然后静默重写 /category/250/category?id=250,从而加载正确的内容并在浏览器中显示漂亮的 URL。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多