EzDevInfo.com

mod-rewrite interview questions

Top mod-rewrite frequently asked interview questions

How can I match query string variables with mod_rewrite?

Suppose I have URLs with query string parameters like these:

/index.php?book=DesignPatterns&page=139
/index.php?book=Refactoring&page=285

Using mod_rewrite, how can I redirect them to SES URLs like these?

/DesignPatterns/139
/Refactoring/285

Source: (StackOverflow)

How to debug Apache mod_rewrite

I have two main problems with mod_rewrite:

1) There is no meaningful error reported when I have an invalid rule

enter image description here

2) To reliably test each modification, I have to erase chrome's cache. This isn't rocket science, but I have to hit Ctrl+Shift+Delete then click OK, then close the window, and reload.

I'd like to see if any of the gurus are willing to share their secrets to efficiently managing mod_rewrite code.


Source: (StackOverflow)

Advertisements

deny direct access to a folder and file by htaccess

Here is the scenario:

  • There is a index.php file in root folder
  • some files are included in index.php which are in the includes folder.
  • 1 other file (submit.php) is in the root folder for form submit action.

I want to restrict direct user access to the files in includes folder by htaccess. also for submit.php. But include will work for index.php file. Like, if user types www.domain.com/includes/somepage.php, it will restrict it (may be redirect to a error page).


Source: (StackOverflow)

URL rewriting with PHP

I have a URL that looks like:

url.com/picture.php?id=51

How would I go about converting that URL to:

picture.php/Some-text-goes-here/51

I think WordPress does the same.

How do I go about making friendly URLs in PHP?


Source: (StackOverflow)

symfony2 rewrite rules .htaccess app.php

I uploaded my symfony2 project to server grove. The main page loads, but all the links are broken. I tried adding app.php to the web address. It did work, but:

I have routes like this one:

www.something.com/app.php/something

I want them to be:

www.something.com/something.

I've been reading, and I should put some rewrite rules on the .htaccess.

I found these rules, but they don't seem to work:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>

Source: (StackOverflow)

.htaccess - how to force "www." in a generic way?

This will change domain.com to www.domain.com:

# Force the "www."
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]

How do I replace the "domain" part so that this works on any domain?


Source: (StackOverflow)

Generic htaccess redirect www to non-www

I would like to redirect www.example.com to example.com. The following htaccess code makes this happen:

RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

But, is there a way to do this in a generic fashion without hardcoding the domain name?


Source: (StackOverflow)

Tips for debugging .htaccess rewrite rules

Many posters have problems debugging their RewriteRule and RewriteCond statements within their .htaccess files. Most of these are using a shared hosting service and therefore don't have access to the root server configuration. They cannot avoid using .htaccess files for rewriting and cannot enable a RewriteLogLevel" as many respondents suggest. Also there are many .htaccess-specific pitfalls and constraints are aren't covered well. Setting up a local test LAMP stack involves too much of a learning curve for most.

So my Q here is how would we recommend that they debug their rules themselves. I provide a few suggestions below. Other suggestions would be appreciated.

  1. Understand that the mod_rewrite engine cycles through .htaccess files. The engine runs this loop:

    do
      execute server and vhost rewrites (in the Apache Virtual Host Config)
      find the lowest "Per Dir" .htaccess file on the file path with rewrites enabled
      if found(.htaccess)
         execute .htaccess rewrites (in the user's directory)
    while rewrite occurred
    

    So your rules will get executed repeatedly and if you change the URI path then it may end up executing other .htaccessfiles if they exist. So make sure that you terminate this loop, if necessary by adding extra RewriteCond to stop rules firing. Also delete any lower level .htaccess rewrite rulesets unless explicitly intent to use multi-level rulesets.

  2. Make sure that the syntax of each Regexp is correct by testing against a set of test patterns to make sure that is a valid syntax and does what you intend with a fully range of test URIs. See answer below for more details.

  3. Build up your rules incrementally in a test directory. You can make use of the "execute the deepest .htaccess file on the path feature" to set up a separate test directory (tree) and debug rulesets here without screwing up your main rules and stopping your site working. You have to add them one at a time because this is the only way to localise failures to individual rules.

  4. Use a dummy script stub to dump out server and environment variables. (See Listing 2)If your app uses, say, blog/index.php then you can copy this into test/blog/index.php and use it to test out your blog rules in the test subdirectory. You can also use environment variables to make sure that the rewrite engine in interpreting substitution strings correctly, e.g.

    RewriteRule ^(.*) - [E=TEST0:%{DOCUMENT_ROOT}/blog/html_cache/$1.html]
    

    and look for these REDIRECT_* variables in the phpinfo dump. BTW, I used this one and discovered on my site that I had to use %{ENV:DOCUMENT_ROOT_REAL} instead. In the case of redirector looping REDIRECT_REDIRECT_* variables list the previous pass. Etc..

  5. Make sure that you don't get bitten by your browser caching incorrect 301 redirects. See answer below. My thanks to Ulrich Palha for this.

  6. The rewrite engine seems sensitive to cascaded rules within an .htaccess context, (that is where a RewriteRule results in a substitution and this falls though to further rules), as I found bugs with internal sub-requests (1), and incorrect PATH_INFO processing which can often be prevents by use of the [NS], [L] and [PT] flags.

Any more comment or suggestions?

Listing 1 -- phpinfo

<?php phpinfo(INFO_ENVIRONMENT|INFO_VARIABLES);

Source: (StackOverflow)

.htaccess rewrite to redirect root URL to subdirectory

Trying to get

www.example.com

to go directly to

www.example.com/store

I have tried multiple bits of code and none work. Please help!

What I've tried:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^(.+)\www.example\.com$
RewriteRule ^/(.*)$ /samle/%1/$1 [L]

What am I doing wrong?


Source: (StackOverflow)

Hidden features of mod_rewrite

There seem to be a decent number of mod_rewrite threads floating around lately with a bit of confusion over how certain aspects of it work. As a result I've compiled a few notes on common functionality, and perhaps a few annoying nuances.

What other features / common issues have you run across using mod_rewrite?


Source: (StackOverflow)

How to check if mod_rewrite is enabled in php?

I was wondering if it is possible to check if mod_rewrite is enabled on Apache AND IIS in PHP.

ModRewrite for IIS exists. Check it here.

So, I'm looking for a PHP script that checks for mod_rewrite on Apache and IIS.

Does anyone know such script or can write one?

Especially for Microsoft IIS.

Thanks!


Source: (StackOverflow)

How to remove "index.php" in codeigniter's path

How do I remove the "index.php" sticking out in every path in codeigniter somewhere in the center? I want clean non index.php-fied URLs?


Source: (StackOverflow)

Do you have to restart apache to make re-write rules in the .htaccess take effect?

I have pushed my .htaccess files to the production severs, but they don't work. Would a restart be the next step, or should I check something else.


Source: (StackOverflow)

Online mod_rewrite testing tool [closed]

mod_rewrite statements can be hard to write and debug, therefore I need a lightweight online tool that enables me to test my RewriteCond, RewriteRule statements on the fly.

Any such tool?


Source: (StackOverflow)

How to enable mod_rewrite for Apache 2.2

I've got fresh install of Apache 2.2 on my Vista machine, everything works fine, except mod rewrite.

I've uncommented

LoadModule rewrite_module modules/mod_rewrite.s

but none of my rewrite rules works, even simple ones like

RewriteRule not_found %{DOCUMENT_ROOT}/index.php?page=404

All the rules I'm using are working on my hosting, so they should be ok, so my question is, is there any hidden thing in apache configuration, that could block mod rewrite?


Source: (StackOverflow)