使用PHP中的.htaccess文件将URL更改为friendly-URL

时间:2017-08-19 13:21:14

标签: php .htaccess

我想更改此网址:

example.com/movie.php?id=50

example.com/movie/50

这是我的.htaccess文件

ErrorDocument 404 http://example.com/error404.php


php_flag display_errors 1


RewriteEngine On
RewriteRule    ^([^/\.]+)/([^/\.]+)$    movie.php?id=$1   [NC,L] 

但它不起作用。

以下是代码的作用:

如果我转到此网址:example.com/movie/50

它会将我重定向到此网址

example.com/movie.php?id=50

我只是不希望它不要重定向我(不要更改URL),并打开相同的内容。

2 个答案:

答案 0 :(得分:0)

试试这个

<?php
function GetURLRewriteArray($url_arr) {
    $url = explode('/', $url_arr);
    unset($url[0]);
    $url = array_values($url);
    $url_array = [];
    for ($i = 0; $i < count($url); $i++) {
        if ($i % 2 == 0) {
            $url_array[$url[$i]] = $url[$i + 1];
        }
    }
    return $url_array;
}
print_r(GetURLRewriteArray($_SERVER['PATH_INFO']));

examples

答案 1 :(得分:0)

首先制作你的htaccess文件的副本并试试这个。我没有尝试过,但可能会有效

Options +FollowSymLinks
RewriteEngine On

# if redirected to url cleaner, redirect to actual lookup file and
# these lines will keep the clean url visible in the browser
RewriteRule ^cat_url_cleaner.php ./cat_lookup.php [L]
RewriteRule ^subcat_url_cleaner.php ./subcat_lookup.php [L]
RewriteRule ^subsubcat_url_cleaner.php ./subsubcat_lookup.php [L]

# do not do anything if already existing file, symbolic link or directory
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]

# redirect clean url (/category) for processing
RewriteRule ^([a-zA-Z0-9_-]+)$ ./cat_url_cleaner.php?c=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ ./cat_url_cleaner.php?c=$1 [L]

# redirect clean url (/category/subcategory) for processing
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$   ./subcat_url_cleaner.php?c=$1&s=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ ./subcat_url_cleaner.php?c=$1&s=$2 [L]

# redirect clean url (/category/subcategory/subsubcategory) for processing
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ ./subsubcat_url_cleaner.php?c=$1&s=$2&ss=$3
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ ./subsubcat_url_cleaner.php?c=$1&s=$2&ss=$3 [L]