Using .htaccess to change directory in url

醉酒当歌 提交于 2019-12-05 23:49:31
Jon Lin

The problem here is that RewriteRule ^blog/(.*)$ /services/$1 [L] internally rewrites the URI, so that the browser doesn't know it's happening, this happens entirely on the server's end. If you want the browser to actually load a different URL, you need to use the R flag like you are in your www redirect, though it's only redirecting requests to root. If you want it to redirect everything to include the "www", you want something like this:

RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

Then to redirect "blog" to "services", just add the R flag (or R=301 if you want the redirect to be permanent).

RewriteRule ^blog/(.*)$ /services/$1 [L,R]

And, if for whatever reason your content isn't actually at /blog/, you need to internally rewrite it back

RewriteCond %{THE_REQUEST} ^GET\ /services/
RewriteRule ^services/(.*)$ /blog/$1 [L]

But this is only if your content is really at /blog/ but you only want to make it appear that it's at /services/.

Actually, in such case, as you have a specific field in Wordpress options to handle the display of a different url, it CAN'T work with .htaccess is the WordPress rules are executed at the end.

And it would be much simpler to use the field "Site Address (URL)" in the General Settings, and enter "mysite.com/services/"

If you don't do that, in spite of your .htaccess, the WP internal rewriting will use you installation repertory

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