使用.htacces重写与查询字符串的URL重写、字符串、htacces、URL

2023-09-02 00:59:00 作者:千屿

我有一个网页,这是www.example.com/page_name.php?var1=dynamic_var1&var2=dynamic_var2

I have a page which is www.example.com/page_name.php?var1=dynamic_var1&var2=dynamic_var2

我要使用.htaccess文件,使它看起来像这样 www.example.com/page_name/dynamic_var1/dynamic_var2

I want to use .htaccess file to make it look like this www.example.com/page_name/dynamic_var1/dynamic_var2

我已经试过这至今

RewriteRule ^page_name/(.*)/(.*)$ page_name.php?var1=$1&var2=$2 [NC]

和当这是怎么回事数据库中的问题是,它是这样的

and the problem is when this is going to database it goes like this

SELECT * FROM table_name WHERE id=dynamic_var1/dynamic_var2.php/dynamic_var1 LIMIT 1

和我也试过这种

RewriteRule page_name/(w+)/(w+) page_name.php?var=$1&var2=$2

它加载的页面,但没有任何图片或CSS文件或任何东西,当我使用萤火它说,它无法加载网址给出。

it is loading the page but without any pictures or css file or anything, and when i used firebug it said that it failed to load the URL given.

当我尝试这样

RewriteRule ^page_name/(w+)/(w+)$ page_name.php?var=$1&var2=$2

这给了我内部服务器错误

It gave me internal server error

在此先感谢,

推荐答案

不要试图从的.htaccess 文件中分割的URL。你为什么不尝试简单地使整个查询字符串的的index.php 或其它任何脚本,你想和解析请求有...

Don't try split the URL from within the .htaccess file. Why don't you try simply passing the entire query string to your index.php or any other script you want and parse the request there...

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

有一个更简单的方法是简单地将所有请求重定向到一个的index.php 文件,做这样的事情 -

A much easier way would be to simply redirect all the requests into an index.php file that does something like this -

的index.php -

由于 http://yourdomain.com/user/123

$originalURL = $_GET['url'];
$URLParts = explode('/',$originalURL);

现在你有 $ URLParts

Array (
 'user',
 '123'
)

现在,您可以轻松地放置在你的SQL查询这些值。

Now you can easily place these values in your SQL query.

当心 SQL注入,但!始终使用 mysql_real_escape_string() 并确保消毒任何输入的数据...

Watch out for SQL injection though! always use mysql_real_escape_string() and make sure to sanitize any incoming data...