Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess Mod_rewrite-将旧URL重定向到新URL_.htaccess_Mod Rewrite_Redirect - Fatal编程技术网

.htaccess Mod_rewrite-将旧URL重定向到新URL

.htaccess Mod_rewrite-将旧URL重定向到新URL,.htaccess,mod-rewrite,redirect,.htaccess,Mod Rewrite,Redirect,我正在寻找一种解决方案,将旧的URL(例如,aboutme.php)重定向到新的URL(相同的/about me) 问题是:如果我转到example.com/aboutme.php,用户不会被重定向到pretty url(/about me)。添加R=301没有帮助-它使/about me重定向到aboutme.php 这是我的.htaccess: <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond

我正在寻找一种解决方案,将旧的URL(例如,
aboutme.php
)重定向到新的URL(相同的
/about me

问题是:如果我转到
example.com/aboutme.php
,用户不会被重定向到pretty url(
/about me
)。添加R=301没有帮助-它使
/about me
重定向到
aboutme.php

这是我的.htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^(.*) http://example.com/$1 [R=301,L]

# Specify search friendly URLs
RewriteRule ^about-me$ /aboutme.php [L]
RewriteRule ^portfolio$ /portfolio.php [L]
RewriteRule ^motion$ /motion.php [L]
RewriteRule ^contact$ /contact.php [L]
RewriteRule ^contact-thanks$ /contact_thanks.php [L]



</IfModule>

重新启动发动机
重写基/
重写cond%{HTTP_HOST}^示例\.com$
重写规则^(.*)http://example.com/$1[R=301,L]
#指定搜索友好的URL
重写规则^about me$/aboutme.php[L]
重写规则^portfolio$/portfolio.php[L]
重写规则^motion$/motion.php[L]
重写规则^contact$/contact.php[L]
重写规则^contact thanky$/contact_thanky.php[L]

你必须朝另一个方向走

如果您想将aboutme.php重写为about me

你应该使用

RewriteRule ^aboutme.php /about-me [R=301,L]
编辑:

关于我的地方必须在场。否则,您可以将这个虚拟位置重定向到一个文件(比如rewrite.php),该文件从数据库或某些配置获取实际文件。将以下规则放在htaccess规则的底部

#don't rewrite the rewrite script
RewriteRule rewrite.php - [L]

#as example don't rewrite the image folder
RewriteRule ^images - [L]

#everything else that doesn't match a rule
RewriteRule ^(.*)$ rewrite.php?file=$1 [QSA,L]
之后,您可以使用
$\u GET['file']
在rewrite.php中访问请求的虚拟文件

编辑:

您的rewrite.php可能如下所示:(这是最简单的方法,但您必须进行一些错误检查等)



谢谢您的回复。不幸的是,现在服务器告诉我“在此服务器上找不到请求的URL/关于我”。好的,假设您转到aboutme.php->您被重定向到不存在的/about我。因此,您必须创建一个与此匹配的规则,并显示正确的内容。你的文件结构和以前一样吗?我应该把rewrite.php规则放在哪里?在.htaccess的底部?我需要改变变量吗?你必须在所有规则之后加上三行。但是要小心,你还必须指定你的图像、css和javascript等所在的所有目录。e、 比如说/image->你必须在最后一行之前定义一个规则
重写规则^images-[L]
。我非常感谢你的回答,但这对我来说太复杂了。互联网上的每个教程都只包含基本的重写规则。所以在现实中,同样的重写规则是不够的?
<?php
$file = $_GET['file'];
$config = array(
    'about-me' => 'aboutme.php'
);
include($config[$file]);
?>