使用mod_rewrite更改站点URL

时间:2023-02-10 11:22:21

I'm finding a lot about creating pretty URIs and whatnot using mod_rewrite but what I'm looking for is a way to remove elements of a URL (as far as the viewer is concerned) and not just moving stuff around and/or reorganizing.

我发现很多关于创建漂亮的URI以及使用mod_rewrite的东西,但我正在寻找的是一种删除URL元素的方法(就观察者而言)而不仅仅是移动东西和/或重新组织。

I need this:

我需要这个:

http://www.mysite.com/foo/bar

to be this:

是这样的:

http://www.mysite.com/bar

Basically, let's get rid of /foo in the URL.

基本上,让我们在URL中删除/ foo。

Is that possible?

那可能吗?

2 个解决方案

#1


You can easily achieve that by simply dropping what you want to lose:

您可以通过简单地删除您想要丢失的内容轻松实现这一目标:

RewriteEngine On
RewriteRule ^foo/(.*)$  $1  [R=301,NE,L]

That should effectively remove foo/ from your URL. If you don't want the address bar to reflect this change, remove R=301. The NE parameter is there to make sure that the request is not escaped.

这应该有效地从您的URL中删除foo /。如果您不希望地址栏反映此更改,请删除R = 301。 NE参数用于确保请求不会被转义。

#2


NOTE: Most of these instructions came from here, "Giving Wordpress its Own Directory".

注意:这些说明中的大部分来自“给Wordpress自己的目录”。

If it is Wordpress specifically, this can be handled by adding an index.php that looks like this to the directory you want to be the base:

如果它是专门的Wordpress,可以通过向你想成为基础的目录添加一个看起来像这样的index.php来处理:

<?php
/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require('./blog2/wp-blog-header.php');
?>

In Wordpress settings, you need to let it know the difference between the "actual" Wordpress URL and the one you want everyone else to use:

在Wordpress设置中,您需要让它知道“实际”Wordpress URL与您希望其他人使用的URL之间的区别:

(These were under "Settings" for me...)

(对我来说,这些都在“设置”下...)

Also, I cannot remember if this is done automatically or not (I thought it was), but the .htaccess where you put the index.php file will need to look like this:

此外,我不记得这是否自动完成(我认为是),但你放入index.php文件的.htaccess将需要如下所示:

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

# END WordPress

#1


You can easily achieve that by simply dropping what you want to lose:

您可以通过简单地删除您想要丢失的内容轻松实现这一目标:

RewriteEngine On
RewriteRule ^foo/(.*)$  $1  [R=301,NE,L]

That should effectively remove foo/ from your URL. If you don't want the address bar to reflect this change, remove R=301. The NE parameter is there to make sure that the request is not escaped.

这应该有效地从您的URL中删除foo /。如果您不希望地址栏反映此更改,请删除R = 301。 NE参数用于确保请求不会被转义。

#2


NOTE: Most of these instructions came from here, "Giving Wordpress its Own Directory".

注意:这些说明中的大部分来自“给Wordpress自己的目录”。

If it is Wordpress specifically, this can be handled by adding an index.php that looks like this to the directory you want to be the base:

如果它是专门的Wordpress,可以通过向你想成为基础的目录添加一个看起来像这样的index.php来处理:

<?php
/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require('./blog2/wp-blog-header.php');
?>

In Wordpress settings, you need to let it know the difference between the "actual" Wordpress URL and the one you want everyone else to use:

在Wordpress设置中,您需要让它知道“实际”Wordpress URL与您希望其他人使用的URL之间的区别:

(These were under "Settings" for me...)

(对我来说,这些都在“设置”下...)

Also, I cannot remember if this is done automatically or not (I thought it was), but the .htaccess where you put the index.php file will need to look like this:

此外,我不记得这是否自动完成(我认为是),但你放入index.php文件的.htaccess将需要如下所示:

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

# END WordPress