Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I need to rewrite a URL of my server so the URL looks friendly. Like:

site.com/var1/var2/var3/...

to

site.com/index.php?page=var1/var2/var3/...

So I wrote this .htacess:

RewriteEngine On
RewriteRule ^.*$ index.php?page=$1 [NC,L]

But when I upload it to the server, and try to load a page, the page loads forever! What am I doing wrong?

share|improve this question
1  
Also note that .htaccess has 2 cs - just in case that isn't a typo in the question! ;) –  w3d Mar 29 at 14:55

1 Answer 1

RewriteRule ^.*$ index.php?page=$1 [NC,L]

This will result in a rewrite loop. You are also not capturing the sub pattern, so $1 will be empty. In order to prevent a rewrite loop, you need a get-out-clause, such as not rewriting when the request is already for index.php.

Something like:

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

Only when the request is not for /index.php will the RewriteRule be processed. The parenthesised sub pattern (.*) is stored in $1.

share|improve this answer
    
I tested this by echo-ing $_GET["page"]; and I always get 'index.php'. What's happening? No matter what I type in it, I always get the index.php. –  Mr Janiqua Mar 31 at 19:44
    
I wanted to type like: site.com/page1/page2/page3 and rewrite to index.php?page=page1/page2/page3 –  Mr Janiqua Mar 31 at 19:45
    
I've just corrected the RewriteRule substitution by prefixing this with a / to make it root-relative. However, it should have still worked from the document root. Yes, this code rewrites site.com/page1/page2/page3 to /index.php?page=page1/page2/page3 (in the document root). –  w3d Mar 31 at 19:56
    
I want all this to ocurr in my test directory named v3, does I need to make the condition to be !=/v3/index.php? I want the index.php?page=... to be on the same root, so I don't need the slahes in it. –  Mr Janiqua Mar 31 at 22:30
    
Yes, if this is all happening in a subdirectory then that directory will need to be prefixed to the REQUEST_URI condition and omit the slash prefix from the RewriteRule substitution. (The subdirectory is automatically removed when matching against the RewriteRule pattern.) –  w3d Mar 31 at 22:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.