消除URL中的目录并重定向?

时间:2015-01-24 11:04:00

标签: .htaccess url-redirection

我想重定向所有的实例:

http://example.com/projects/title-of-project

要:

http://example.com/title-of-project

我该怎么做?

htaccess的

# WordPress
<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>

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

# END WordPress

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

1 个答案:

答案 0 :(得分:1)

您可以将此代码放入htaccess(在根文件夹中)

# BEGIN WordPress
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /wordpress/

  RewriteRule ^projects/(.*)$ $1 [R=301,L]
  RewriteRule ^index\.php$ - [L]

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^ index.php [L]
</IfModule>
# END WordPress

确保 mod_rewrite 已启用(并允许htaccess个文件)