Apache的URL重写/重定向与数据库中的数据 [英] Apache URL rewrite/redirection with database data

查看:135
本文介绍了Apache的URL重写/重定向与数据库中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的PHP code(qs.php)。此文件包含pretty的URL链接。

Here is my php code(qs.php). This file contain pretty url links.

<html>
<head></head>
<body>
    <?php  
    $file = 'qs1'; //this is a php file qs1.php
    $id = '12345678'; //testing ID
    $complete_url = $file."/".$id;

    ?>
    <a href ="<?php echo $complete_url;?>"> This is a test link </a>

</body></html>

这个环节出现的链接这一点 - 的http://本地主机/ QS1 / 12345678

This link appear link this - http://localhost/qs1/12345678

QS1是一个PHP文件(qs1.php)。

QS1 is a php file (qs1.php).

下面是htaccess文件为止。

Below is the htaccess file so far.

RewriteEngine On
RewriteBase /

RewriteRule ^([^/]+)/(\d+)/([^/]+)$ $qs1.php?var1=$2 [NC,L]

在qs1.php文件中我得到的查询字​​符串 VAR1 $ _ GET ['VAR1']

In qs1.php file i am getting query string var1 by $_GET['var1']

我想环节都能通过此网址访问的http://本地主机/ QS1 / 12345678 。 如果用户把斜线结束这样的的http://本地主机/ QS1 / 12345678 / 然后重定向页面本身这个的http://本地主机/ QS1 / 12345678

I want link can be accessible by this url http://localhost/qs1/12345678. and if user put slash at the end like this http://localhost/qs1/12345678/ then page redirect itself to this http://localhost/qs1/12345678.

目前,我收到错误404,同时打开该的http://本地主机/ QS1 / 12345678 /

At the moment i am getting error 404 while opening this http://localhost/qs1/12345678/

感谢您的评论。

推荐答案

您的规则不匹配,因为它会等待后面的数字
别的东西 例如 http://example.com/qs1/12345678/something

Your rule does not match since it waits for something else after digits
e.g. http://example.com/qs1/12345678/something

您可以在您的htaccess的使用code

You can use this code in your htaccess

Options -MultiViews

RewriteEngine On
RewriteBase /

# we don't touch existing files/folders
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# remove trailing slash (for instance: redirect /qs1/12345678/ to /qs1/12345678)
RewriteRule ^([^/]+)/(\d+)/$ $1/$2 [R=301,L]

# rewrite /xxx/12345 to /xxx.php?var1=12345 (if xxx.php exists)
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^([^/]+)/(\d+)$ $1.php?var1=$2 [L]

# rewrite /xxx/12345/something to /xxx.php?var1=12345&var2=something (if xxx.php exists)
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^([^/]+)/(\d+)/([^/]+)$ $1.php?var1=$2&var2=$3 [L]

这篇关于Apache的URL重写/重定向与数据库中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆