dqc3469 2018-01-17 05:50
浏览 62
已采纳

将example.com重定向到example.com/blog

I have uploaded my website under a sub-directory on my server the structure is looks something like this

root

----blog

--------.htaccess

----apple

----.htaccess

Now I have written a rule inside my /blog/.htaccess file which will redirect users to

https://www.example.com/blog/

I have done this by adding the following rule.

RewriteEngine On
RewriteBase /blog/

RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

so far so good it is working exactly like the requirements, Now the real issue come. User wants that when ever any one hit the domain without the preceding blog it will add the /blog/ and redirects the user to the blog site and this rule should not effect any sub-domain means if

User enters the url.

example.com

He should be redirected to.

https://www.example.com/blog/

And when he enters url.

apple.example.com

He should redirected to

apple.example.com

Now In order to do so I am writing the following rule but it is not working the rule looks like this.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) https://example.com/$1 [R=301,L]
RewriteRule ^$ /blog/ [L]

I don't know what I am doing wrong.

I have added so much detail because I thought may be the .htaccess file inside the blog directory is making an issue.

  • 写回答

2条回答 默认 最新

  • dongqie4233 2018-01-17 06:09
    关注

    Found the issue with the help of @Twinkle the real issue was that I was adding

    RewriteRule ^$ /blog/ [L]
    

    which I didn't needed for obvious reasons by removing this rule and changing my current rule to the following rule resolved my problem.

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^example\.com$
    RewriteRule (.*) https://example.com/blog/ [R=301,L]
    
    RewriteCond %{HTTP_HOST} ^www.example\.com$
    RewriteRule (.*) https://example.com/blog/ [R=301,L]
    

    Hope some one find this help full.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。