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 url, it will look like d [] = folder1 & d [] = folder2 & d [] = folder3

Is it possible?

Thanks.

+4
source share
5 answers

Yes, it is possible:

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

But mod_rewrite is not really suitable for this kind of work. In fact, you can quickly create an endless loop with the N flag .

You should use PHP to retrieve the requested URL for its segments:

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

Then use this one rule to overwrite requests in a file:

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

I don’t know how to do this automatically, but you can get the requested URL from $ _SERVER ['REQUEST_URI'] and do your own string processing to extract the information you need.

+2
source

This is what I do for Seach Engine Friendly URLs with unlimited levels. It also gives you the option to allow query strings or not and will not rewrite URLs for real folders or files such as images, CSS and 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 /* 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; } ?> 

Then I have a routing file that processes the request using the appropriate functions. This may seem like a lot of code, but it helps keep everything organized and efficient, as I only include the classes / functions that the request requires.

I am considering releasing a library with code (Urgh, not another frame that I hear), I use for my projects, so feel free to use the code under GPL v3.

Hope this helps.

+2
source

This should give you an idea of ​​what you 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, this will be a completely different problem.

0
source

A good technique to use is the front controller (part of the MVC design pattern). The front controller is a (PHP) file that is sent to every request that your website receives. The front controller sends a request to other files depending on the type of request (see Page X, view of form Y, etc.). For example, you can β€œcut off” the request URL using the method described by @Ray Hidayat and look at it to determine which part of your site should process and / or respond to the request.

For example: Zend Framework and Drupal use this technique. In these cases, and I think that in most cases index.php in the root of the site is the front controller, so www.example.com/index.php

You can use mod_rewrite to route each request to the front controller.

 RewriteEngine on RewriteRule .* index.php 

You can use the following mod_rewrite script for images / files / CSS and javascript.

 RewriteEngine off 

Good luck

0
source

All Articles