Friendly URLs and htaccess levels

I have this .htaccess

RewriteEngine On RewriteRule ^custom index.php?page=value [NC,L] 

It works if index.php and .htaccess are in the same folder.

I would like to put the .htaccess file in the root directory and make the content placed in newFolder respond to the htaccess rules. Say:

.htaccess -> root level
index.php -> root / newFolder level

I tried:

 RewriteEngine On RewriteRule ^custom newFolder/index.php?page=value [NC,L] 

No reaction at all (404): (

I think the solution should be simple, but ...

Any help would be appreciated.

+7
php apache .htaccess mod-rewrite
source share
2 answers

Make sure that we are on the same page .;) In my server root directory I have a .htaccess file, and in this file I have:

 RewriteEngine On RewriteRule ^(.+/)?custom $1index.php?page=value [NC,L] 

RewriteRule has three components: a matching pattern, a target, and a list of flags. A match pattern can contain plain text or a regular expression. The target may contain plain text or text with interleaved variables recorded in the regular expression matching pattern. Both the match patterns and the target refer to the directory in which the .htaccess file is located.

The matching pattern is compared with the text in the URL that is after the current directory. If there is a successful match, then the target is called from the directory in which the .htaccess file is located. If the information falls into the matching pattern, then the variables in the target are first interpolated to the target.

In this example, the .htaccess file is located in the root of the server, so the matching pattern is compared with the text of the URL after: http://localhost/

To test this, here is the PHP code that I entered in the index.php file:

 <?php echo 'page value: ' .$_GET['page'] . '<br>'; echo 'script loaded: ' . $_SERVER['PHP_SELF']; ?> 

When I go to http://localhost/custom in my browser: the regex matching pattern is compared to the string "custom", a successful match is made there, so that the target index.php?page=value is executed, and I get the index.php page The root directory is loaded. The page displayed is as follows:

 page value: value script loaded: /index.php 

Then I create a new directory called "newFolder" in the root of the server and copy the index.php file into the newly created directory.

When I go to http://localhost/newFolder/custom in my browser: the regular expression matching pattern is compared with the string "newFolder / custom", the match is successful there again, this time the target newFolder/index.php?page=value is executed newFolder/index.php?page=value , and I get index.php in the newFolder directory. The page displayed is as follows:

 page value: value script loaded: /newFolder/index.php 

Thus, I can create any number of directories in the root directory and the new index.php files in these directories, and when navigating the "normal" in these directories, this .htaccess file should load the corresponding index. php in the current subdirectory.

~~~

I am going to reach a limb here and assume that you want to dynamically load pages, so fix “custom” as the dynamic name of the page and upload it to the page field (replace “value”) and send this index.php file in the current subdirectory.

 RewriteEngine On RewriteCond %{REQUEST_URI} !index\.php RewriteRule ^(.+/)?(.+)$ $1index.php?page=$2 [NC,L] 

As already mentioned, parentheses capture dynamic text and unload it in the target line, where the dollar signs are indicated. Thus, the text recorded in the first set of parentheses replaces "$ 1", and the text recorded in the second set replaces "$ 2". I also added a RewriteCond statement so that this rule is not duplicated. :) If I do not make this rule, it is executed both for a "custom" call and for redirecting to index.php.

So, now when I go to http://localhost/newFolder/whoopdeedoo , the http://localhost/newFolder/whoopdeedoo pattern is compared to "newFolder / whoopdeedoo", there is a successful match, the target is newFolder/index.php?page=whoopdeedoo , and I get :

 page value: whoopdeedoo script loaded: /newFolder/index.php 

~~~

At the risk of a buzz, here is some background information. You mentioned that this works in the root directory:

 RewriteEngine On RewriteRule ^custom index.php?page=value [NC,L] 

But this does not work when navigating your browser to subdirectories. After reading the comments, it turned out that this next bit works for subdirectories, in particular "newFolder":

 RewriteEngine On RewriteRule ^newFolder/custom newFolder/index.php?page=value [NC,L] 

But now it no longer works for the root directory. Therefore, you can simply include both:

 RewriteEngine On RewriteRule ^custom index.php?page=value [NC,L] RewriteRule ^newFolder/custom newFolder/index.php?page=value [NC,L] 

However, there are a couple of things that jump on me. One of them is that instead of specifying the hardcoding directory names in my root .htaccess, I prefer to put .htaccess files in each subdirectory and handle the URLs there.

But if for any reason you prefer to maintain a single .htaccess file in the root directory, then what jumps at me is duplication in the rules. So you can use regular expressions to make the subdirectory the name of the captured variable, as shown at the top of this answer:

 RewriteEngine On RewriteRule ^(.+/)?custom $1index.php?page=value [NC,L] 

Brackets that copy regular expressions replace the text "newFolder /", and inside the parentheses we say: "Grab one or more characters followed by a slash." Then, if there is a successful match, the resulting value replaces "$ 1" in the target. The reason this also works in the root is because the question mark following the parentheses makes the regular expression inside the parens options optional. Therefore, if there was nothing before the “custom”, we are still good for the match. In this case, "$ 1" is replaced by an empty string.

+6
source share

Try with this rewriterule :

 # Activate RewriteEngine RewriteEngine on # Rewrite the URL requested by the user # Entry: folder/clients/name/ # Output: folder/clients.php?id=name RewriteRule ^folder/clients/(\w+)/?$ /folder/clients.php?id=$1 

Explanation of this rewritable line:

The first part of the rewriterule :

  • ^ Top expression
  • folder/clients/ The requested URL string starts with folder/clients/
  • (\w+) Capture all subsequent letters and save them in $1
  • /? Extra backslash at the end of the URL
  • $ End of expression

Second part of rewriterule :

  • clients.php?id= Text string
  • $1 The first capture we saw in the first part
+5
source share

All Articles