简体   繁体   English

Apache mod_rewrite和PHP GET Arrays

[英]Apache mod_rewrite and PHP GET Arrays

I want to have a url like http://localhost/folder1/folder2/folder3/file Then i want mod_rewrite to convert it to $_GET['d'] in the url it would look like d[]=folder1&d[]=folder2&d[]=folder3 我想要一个像http:// localhost / folder1 / folder2 / folder3 / file这样的网址然后我希望mod_rewrite将它转换为$ _GET ['d'],它看起来像d [] = folder1&d [] =文件夹2&d [] = folder3

Is this possible? 这可能吗?

Thank You. 谢谢。

Yes it is possible: 对的,这是可能的:

RewriteRule ^(([^/]+/)*)([^/]+)/([^/]+)$ /$1$4?d[]=$3 [QSA,N]
RewriteRule ^([^/]+)$ - [L]

But mod_rewrite is not really suited for that kind of work. 但mod_rewrite并不适合这种工作。 In fact, you can quickly create an infinite loop with the N flag . 实际上,您可以使用N标志快速创建无限循环。

You should rather use PHP to extract the requested URL path its segments: 您应该使用PHP来提取其段所请求的URL路径:

$path = strtok($_SERVER['REQUEST_URI'], '?');
$segments = implode('/', ltrim($path, '/'));

Then use this single rule to rewrite the requests to your file: 然后使用此单个规则重写对您的文件的请求:

RewriteRule ^([^/]+/)+([^/]+)$ $2 [L]

我不知道自动执行此操作的方法,但您可以从$ _SERVER ['REQUEST_URI']检索所请求的URL,并对其执行自己的字符串处理以提取所需的信息。

This is what I do for Seach Engine Friendly URLs with unlimited amount of levels. 这就是我为Seach Engine Friendly URL所做的事情,它具有无限的级别。 It also gives you the option of allowing query strings or not, and will not rewrite urls to real folders or files like images, CSS and JavaScript. 它还为您提供了允许查询字符串的选项,并且不会将URL重写为真实文件夹或文件,如图像,CSS和JavaScript。

Apache... 阿帕奇...

# Do not use htaccess if you can avoid it, instead write all of this in the httpd.conf <VirtualHost /> and disable .htaccess for performance/security.
RewriteEngine On
RewriteBase /

# Redirect non-www traffic to www.domain.co.uk/request
RewriteCond %{HTTP_HOST} !^www\.domain\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [R=301,L]

# Do not rewrite real files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*/ - [L]

# Use 1 or 2 Below

# 1. SEO Friendly URLs (don't allow additional query strings /foo/bar/)
# RewriteRule ^([A-Za-z0-9/-]*)$ index.php?request=$1 [L]

# 2. SEO Friendly URLs (allow additional query strings /foo/bar/?q=search)
RewriteRule ^([A-Za-z0-9/-]*)$ index.php?request=$1 [L,QSA]

PHP... PHP ...

<?php

/*
    Title: Request
    Gets the client request and sanitizes the user input.

    Returns:
        Array of parts of the URL.

        > $request = /path/to/page/
        > var_dump($request);
        > array(3) { [0]=>  string(4) "path" [1]=>  string(2) "to" [2]=>  string(4) "page" }
*/

// Check request exists
if (isset($_GET['request']) && !empty($_GET['request']))
{
    // Yes. Sanitize request.
    // request should be made lowercase, as URLs should not be case-sensitive.
    $request = strtolower($_GET['request']);

    // Sanitize request incase the user tries to circumvent the .htaccess rules and uses the query string directly. index.php?request=
    $request = preg_replace("([^a-z0-9-/])", '', $request);

    // Check if request ends with trailing slash to ensure all crawled URLs end with a trailing slash. ($request does not include other query strings or anchors)
    if ((substr($request, -1, 1)) == '/')
    {
        // Yes, request should now be safe to use.
        $safe['url'] = $request;

        // Split request into an array with values for each directory.
        $safe['request'] = explode('/', $request, -1);

        // Destroy user input to prevent usage.
        unset($_GET['request'], $request);
    }
    else
    {
        // No, redirect to request with trailing slash.
        header('Location: /' . $request . '/', true, 301);
        exit;
    }
}
else
{
    // No.
    $safe['request'] = false;
}

?>

I then have a routing file which handles the request with the appropriate functions. 然后我有一个路由文件,用适当的函数处理请求。 It might seem like a lot of code but helps keep things organised and efficient as I only include the classes/functions that the request requires. 它可能看起来像很多代码但有助于保持组织和效率,因为我只包含请求所需的类/函数。

I'm considering releasing a library of code (Urgh, not another framework I hear you cry) I use for my projects so feel free to use the code under GPL v3. 我正在考虑发布一个代码库(Urgh,而不是我听到你哭的另一个框架)我用于我的项目,所以随意使用GPL v3下的代码。

Hope this helps. 希望这可以帮助。

This should give you a rough idea of what you'd need... 这应该会让你大致了解你需要什么......

Rewrite Rule ^(.*)/(.*)/(.*)/(.*)$ /my_script.php?d[]=$1&d[]=$2&d=$3&f=$4

If you want to support an arbitrary number of directories, rather than 3 fixed directories, that'll be another issue altogether. 如果你想支持任意数量的目录,而不是3个固定目录,这将是另一个问题。

A good technique to use is a front controller (part of the MVC design pattern). 一个好的技术是前端控制器(MVC设计模式的一部分)。 A front controller is a (PHP) file to which every request that your website gets is routed. 前端控制器是一个(PHP)文件,您的网站获取的每个请求都将被路由到该文件。 The front controller then dispatches the request to other files depending on the kind of request (look at page X, submission of form Y, etc). 然后,前端控制器根据请求的类型将请求分派给其他文件(查看第X页,提交表单Y等)。 For example you can "cut up" the url of the request, using the technique described by @Ray Hidayat, and look at it to determine which part of your site should handle and or answer the request. 例如,您可以使用@Ray Hidayat描述的技术“剪切”请求的U​​RL,并查看它以确定您的站点的哪个部分应该处理和/或回答请求。

For example: Zend Framework and Drupal both use this technique. 例如:Zend Framework和Drupal都使用这种技术。 In those cases, and I think in most cases, index.php in the root of the site is the front controller, so www.example.com/index.php 在这些情况下,我认为在大多数情况下,站点根目录中的index.php是前端控制器,所以www.example.com/index.php

You can use mod_rewrite to route every request to that the front controller. 您可以使用mod_rewrite将每个请求路由到前端控制器。

RewriteEngine on
RewriteRule .* index.php

You can use the following mod_rewrite script for images/files/CSS and javascript library. 您可以对images / files / CSS和javascript库使用以下mod_rewrite脚本。

RewriteEngine off

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM