使用的.htaccess更改URL目录 [英] Using .htaccess to change directory in url

查看:160
本文介绍了使用的.htaccess更改URL目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图来改变显示在从mysite.com/blog/wedding-hair/地址栏中的网址使用的.htaccess mysite.com/services/wedding-hair/~~V。

I am trying to change the url that is displayed in the address bar from mysite.com/blog/wedding-hair/ to mysite.com/services/wedding-hair/ using .htaccess.

使用答案来自: <一href="http://stackoverflow.com/questions/8713319/assigning-different-name-to-existing-folder-in-url-in-htaccess">assigning不同的名称在URL中的htaccess 现有的文件夹,使用重写一个文件夹名称。 htaccess的和<一href="http://stackoverflow.com/questions/10071404/replace-directory-name-in-url-with-another-name">Replace在URL与我加入到.htaccess文件的另一个名称目录名。下面是.htaccess文件,我说的最后重写规则:

Using answers from: assigning different name to existing folder in url in htaccess, rewrite a folder name using .htaccess, and Replace directory name in url with another name I added to the .htaccess file. Here is the .htaccess file, I added the last rewrite rule:

Options -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule ^/?$ "http\:\/\/www\.mysite\.com" [R=301]
RewriteRule ^blog/(.*)$ /services/$1 [L]

非www重定向的作品而不是博客-服务重写。我想,也许我已经扭转,但周围改变它们不能正常工作或目录名。我曾尝试添加和在各种不同的组合,删除/的周围的目录名。我尝试添加

the non-www redirect works but not the blog-services rewrite. I thought maybe I had the directory names reversed but changing them around doesn't work either. I have tried adding and removing /'s around the directory names in all of the different combinations. I tried adding

RewriteCond %{THE_REQUEST} ^GET\ /blog/

我重写规则之前。没有什么我曾尝试工作过,所显示的URL仍然mysite.com/blog/wedding-hair /

before my RewriteRule. Nothing I Have tried has worked, the displayed url remains mysite.com/blog/wedding-hair/

我相信这是pretty的直线前进的人,但我无法得到这个正确的。任何帮助将是AP preciated。

I am sure this is pretty straight forward for someone but I am unable to get this correct. Any help would be appreciated.

在我工作的这个昨天我没有想到的是,博客的目录是一个字preSS安装。下面是.htaccess文件是在博客目录:

When I was working on this yesterday I didn't think about the fact that the blog directory is a WordPress install. Here is the .htaccess file that is in the blog directory:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress

我曾尝试加入我重写规则在这个文件中,但仍然没有喜悦。

I have tried adding my RewriteRule in this file but still no joy.

推荐答案

这里的问题是,重写规则^博客/(.*)$ /服务/ $ 1 [L] 内部重写URI,从而使浏览器不知道它的发生,这种情况完全在服务器端。如果您希望浏览器实际加载不同的URL,你需要使用研究标记就像你在你的WWW重定向,但它只是将请求重定向到根。如果你想让它重定向一切,包括www的,你想是这样的:

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} ^mysite.com$
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]

然后重定向博客为服务,只需添加研究标记(或 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 you're 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 you're content is really at /blog/ but you only want to make it appear that it's at /services/.

这篇关于使用的.htaccess更改URL目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆