将旧网址重写为漂亮的网址,并使用.htaccess将旧网址重定向到新网址

时间:2016-07-19 08:26:26

标签: apache .htaccess mod-rewrite url-redirection friendly-url

mod_rewrite设置存在问题。 我的网站首页网址为:http://www.domain.com/b/en/index.php,我使用RewriteRule ^en/(.*)$ /b/$1 [L,QSA,NC,PT] 创建了漂亮的网址:http://www.domain.com/en

这是我对漂亮网址的重写规则(可行):

www.domain.com redirected you too many times.

这就是我想要的东西,但是当我将“旧网址”重定向到新网址时,存在一些问题。

我的Chrome浏览器显示“RewriteRule ^b/en/(.*)$ /en/$1 [NC,L,R=301]

这是我的旧网址重定向到新规则:

$target_dir = "/var/www/uploads/";
$target_file = $target_dir . $reg_id ."&".$to ."&". basename($_FILES["file"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);


// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, the file already exists.";
    $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error    
if ($_FILES['file']['error'] === UPLOAD_ERR_OK) { 
    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
    // if everything is ok, try to upload file
    } else {
        if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
            echo "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file to dir: $target_file.";
        }
    }
} else { 
    throw new UploadException($_FILES['file']['error']); 
} 

class UploadException extends Exception 
{ 
    public function __construct($code) { 
        $message = $this->codeToMessage($code); 
        parent::__construct($message, $code); 
    } 

    private function codeToMessage($code) 
    { 
        switch ($code) { 
            case UPLOAD_ERR_INI_SIZE: 
                $message = "The uploaded file exceeds the upload_max_filesize directive in php.ini"; 
                break; 
            case UPLOAD_ERR_FORM_SIZE: 
                $message = "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form";
                break; 
            case UPLOAD_ERR_PARTIAL: 
                $message = "The uploaded file was only partially uploaded"; 
                break; 
            case UPLOAD_ERR_NO_FILE: 
                $message = "No file was uploaded"; 
                break; 
            case UPLOAD_ERR_NO_TMP_DIR: 
                $message = "Missing a temporary folder"; 
                break; 
            case UPLOAD_ERR_CANT_WRITE: 
                $message = "Failed to write file to disk"; 
                break; 
            case UPLOAD_ERR_EXTENSION: 
                $message = "File upload stopped by extension"; 
                break; 

            default: 
                $message = "Unknown upload error"; 
                break; 
        } 
        echo $message;
        return $message; 
    } 
}     
?>

2 个答案:

答案 0 :(得分:0)

这是一个通用的两部分问题。您需要将原始请求重定向到更漂亮的请求:

RewriteRule ^en/(.*)$ /b/en/$1 [L,QSA,NC]

RewriteCond %{THE_REQUEST} ^GET\ /b/(en)/(\S*) [NC]
RewriteRule ^b/ /%1/%2 [R=301,L]

务必清除浏览器'在测试新规则之前缓存。

答案 1 :(得分:0)

我有一个更通用的用例,我不得不修改@ hjpotter92的解决方案。

旧网址结构:http://example.com/index.php?page=foo
新的网址结构:http://example.com/foo

所以我想让网址“漂亮”(即搜索引擎优化友好),但也让旧的(丑陋的)网址重定向到新的网址,这样旧的书签仍然可以使用。

这是我的解决方案:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/?(.*)$ /index.php?page=$1 [L,QSA,NC]

RewriteCond %{THE_REQUEST} ^GET\ \/?index\.php\?page=(\S*) [NC]
RewriteRule ^index.php /%1? [R=301,L]