dongpigui8898 2016-07-14 10:29
浏览 32
已采纳

如何在htaccess中将www重定向到没有www的内页

I was working on web based ifsc code search engine called Myifsccodes.com. I am the only person who developed it fully using Php and mysql. I made it redirect from WWW version to without WWW for home page in Bluehost Cpanel that uses htaccess 301 redirect. Its working fine for home page. But recently i found that, if i put www for inner pages they are not redirecting to the without WWW version, which is a huge duplicate problem for site's SEO.

I tried various fixes from Stackoverflow other similar questions, but nothing works accurately. This question is my last hope to fix it with experts here.

Here is my current .htaccess code

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.myifsccodes\.com$
RewriteRule ^/?$ "http\:\/\/myifsccodes\.com\/" [R=301,L]

# Use PHP54 Single php.ini as default
AddHandler application/x-httpd-php54s .php

# For security reasons, Option followsymlinks cannot be overridden.
Options +SymLinksIfOwnerMatch

RewriteCond %{REQUEST_FILENAME} !-f

# data compression
#AddOutputFilterByType DEFLATE text/html text/plain text/xml text/javascript text/css application/javascript

# Url format sample http://myifsccodes.com/example-bank

RewriteRule ^([a-zA-Z0-9_-]+)/$ /bankdetails2.php?banknamefromhtaccess=$1
RewriteRule ^([a-zA-Z0-9_-]+)$ /bankdetails2.php?banknamefromhtaccess=$1

I want to achieve below redirection;

My preferred version of this page http://myifsccodes.com/canara-bank

And this URL http://www.myifsccodes.com/canara-bank should redirect to above URL.

Similarly, 2nd level sub folder URLs should also redirect to non-www version of their respective page.

Example: This URL http://www.myifsccodes.com/canara-bank/delhi should redirect to this one http://myifsccodes.com/canara-bank/delhi

Please help me fix this issue as soon as possible.

  • 写回答

1条回答 默认 最新

  • drmticpet66231422 2016-07-14 10:40
    关注

    Try the following

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]
    RewriteCond %{HTTP_HOST} ^www\.myifsccodes\.com$
    RewriteRule ^/?$ "http\:\/\/myifsccodes\.com\/" [R=301,L]
    

    Into

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)