htaccess internal rewrite from subdomain to subdir path

房东的猫 提交于 2019-12-10 23:30:47

问题


Im at least 24 hours busy on this subject, I cant seem to get this script to work. The script doesnt redirect at all.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^m\.skynet\.com$
RewriteRule ^/(.*)$ /mobile/final/$1 [L,NC]

What am I doing wrong?

UPDATE:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^m\.skynet\.com$
RewriteCond %{REQUEST_URI} !^/mobile/final/
RewriteRule ^(.*)$ /mobile/final/$1 [L,NC]

This partially works, but links like m.skynet.com\download.php wont get redirected

UPDATE: 45min later

Now it suddenly works. But the URL gets rewritten in FireFox, while in Chrome it works properly

UPDATE Seems that the problem with Firefox was caused by a 301-redirect I used earlier, the cache had to be flushed. for it to work properly again


回答1:


Remove leading slash from match:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^m\.skynet\.com$
RewriteCond %{REQUEST_URI} !^/mobile/final/
RewriteRule ^(.*)$ /mobile/final/$1 [L,NC]
  • .htaccess is per directory directive and Apache strips the current directory path (thus leading slash) from RewriteRule URI pattern.
  • You need RewriteCond %{REQUEST_URI} !^/mobile/final/ to prevent looping.


来源:https://stackoverflow.com/questions/21655889/htaccess-internal-rewrite-from-subdomain-to-subdir-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!