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

All .php files are rewritted before doing the below rewriting:

test.php => test
foo.php => foo

I want to rewrite this URL http://www.testdomain.com/test?name=john&id=2&nick=doe to http://www.testdomain.com/test/john/2/doe.

test is the index file.

I tried these methods to rewrite:

  1. Method 1:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/([^/]+)/(.*)$ test?name=$1&id=$2&nick=$3 [L,QSA]
    
  2. Method 2:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/([^/]+)/(.*)$ test.php?name=$1&id=$2&nick=$3 [L,QSA]
    
  3. Method 3:

    RewriteEngine On
    RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /test?name=$1&id=$2&nick=$3 [L]
    

updated .htaccess file content:

RewriteEngine On

RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]

# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^name=([\w]+)\&id=([\d]{1,3})\&nick=([\w]+)$
RewriteRule .* /test/%1/%2/%3? [R=301,L]

All of them are not working (URL remains the same they don't take effect)! What am I doing wrong?

share|improve this question
    
"not working" - in what way "not working"? Incorrect rewrite? Rewrite loop? "All php files are rewritted before doing the below rewriting" - I assume you mean "redirected"? This could be the problem and could be resulting in a rewrite loop - can you paste your entire .htaccess file. You also probably want to be rewriting to a root-relative URL, rather than a relative URL. ie. /test.php?..., not test.php?.... #2 would seem to be the closest to "correct". –  w3d Mar 17 at 14:40
    
@w3d see the edit –  Ancient Geek Mar 17 at 14:45
    
are you sure that server is parsing your .htaccess at all? Sometimes you can only set it in apache configuration. –  edvinas.me Mar 17 at 18:14
    
@edvinas.me yeah of course it's parsing the URL rewrite because .php is removed from every php file when a php file is accessed !!! –  Ancient Geek Mar 17 at 18:21
    
We can not work out a reliable rule without knowing what you have in your .htaccess file, so any answer is going to start from testdomain.com/test.php?name=john&id=2&nick=doe which may or may not suit you. The only way for you to get a good answer is to post here your .htaccess file or use the whole rewrite that somebody writes. –  PatomaS Mar 27 at 10:54
show 4 more comments

5 Answers 5

You mentioned that two options are valid for you, so I'm using the second one, http://www.testdomain.com/john/2/doe, and assuming a clean .htaccess

RewriteEngine On
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^name=([\w]+)\&id=([\d]{1,3})\&nick=([\w]+)$
RewriteRule .* /%1/%2/%3? [R=301,L]

The commented lines that check for files or directories may be uncommented depending on your situation, lets assume that a request on your site is going to be like this

http://www.example.com/test.php?name=john&id=2&nick=doe

If you have the file test.php and you uncomment the line about checking for existing files, the rule I wrote will fail because of unmet conditions. If you don't have that file, you can uncomment the line. The same applies for directories, although safer since I don't think you have directories with extensions.

The above rules transform

http://www.example.com/test?name=john&id=2&nick=doe

into

http://www.example.com/john/2/doe

I tested it on a virtual server with Apache 2.2.25 on Linux.

Below is the rewrite log of the test in case you want to analyse it. The name of the folder and local virtual server where different, I hope I didn't alter anything important when renaming that information.

127.0.0.1 - - [28/Mar/2014:00:03:50 +0800] [www.example.com/sid#cee650][rid#7fe164002970/initial] (3) [perdir /mnt/webs/folder/] strip per-dir prefix: /mnt/webs/folder/test.php -> test.php
127.0.0.1 - - [28/Mar/2014:00:03:50 +0800] [www.example.com/sid#cee650][rid#7fe164002970/initial] (3) [perdir /mnt/webs/folder/] applying pattern '.*' to uri 'test.php'
127.0.0.1 - - [28/Mar/2014:00:03:50 +0800] [www.example.com/sid#cee650][rid#7fe164002970/initial] (2) [perdir /mnt/webs/folder/] rewrite 'test.php' -> '/john/2/doe?'
127.0.0.1 - - [28/Mar/2014:00:03:50 +0800] [www.example.com/sid#cee650][rid#7fe164002970/initial] (3) split uri=/john/2/doe? -> uri=/john/2/doe, args=<none>
127.0.0.1 - - [28/Mar/2014:00:03:50 +0800] [www.example.com/sid#cee650][rid#7fe164002970/initial] (2) [perdir /mnt/webs/folder/] explicitly forcing redirect with http://www.example.com/john/2/doe
127.0.0.1 - - [28/Mar/2014:00:03:50 +0800] [www.example.com/sid#cee650][rid#7fe164002970/initial] (1) [perdir /mnt/webs/folder/] escaping http://www.example.com/john/2/doe for redirect
127.0.0.1 - - [28/Mar/2014:00:03:50 +0800] [www.example.com/sid#cee650][rid#7fe164002970/initial] (1) [perdir /mnt/webs/folder/] redirect to http://www.example.com/john/2/doe [REDIRECT/301]
127.0.0.1 - - [28/Mar/2014:00:03:50 +0800] [www.example.com/sid#cee650][rid#7fe164006990/initial] (3) [perdir /mnt/webs/folder/] add path info postfix: /mnt/webs/folder/john -> /mnt/webs/folder/john/2/doe
127.0.0.1 - - [28/Mar/2014:00:03:50 +0800] [www.example.com/sid#cee650][rid#7fe164006990/initial] (3) [perdir /mnt/webs/folder/] strip per-dir prefix: /mnt/webs/folder/john/2/doe -> john/2/doe
127.0.0.1 - - [28/Mar/2014:00:03:50 +0800] [www.example.com/sid#cee650][rid#7fe164006990/initial] (3) [perdir /mnt/webs/folder/] applying pattern '.*' to uri 'john/2/doe'
127.0.0.1 - - [28/Mar/2014:00:03:50 +0800] [www.example.com/sid#cee650][rid#7fe164006990/initial] (1) [perdir /mnt/webs/folder/] pass through /mnt/webs/folder/john

Update according to your comment If you want test on the final URL, use this:

RewriteRule .* /test/%1/%2/%3? [R=301,L]

If you want your index file, use this:

RewriteRule .* /index.php/%1/%2/%3? [R=301,L]
share|improve this answer
    
It works no problem but i want test or any other file name in my url. is there a way to do that ? –  Ancient Geek Mar 27 at 16:30
    
A random word can not be there, if you want test, I'll update my answer with that. –  PatomaS Mar 27 at 23:28
    
now i am getting this error "The requested URL /test/alex/1/ax.php was not found on this server." apache appending .php to the last parameter. i don't know why apache is doing this . –  Ancient Geek Mar 28 at 7:13
    
url rewriting is working but apache appending .php –  Ancient Geek Mar 28 at 7:13
    
check the update .htaccess this is happening after i remove .php from all files –  Ancient Geek Mar 28 at 7:26
add comment

I had a similar rule, try this one:

RewriteEngine On
RewriteBase /
RewriteRule ^test/([A-Za-z0-9-]+)/([0-9]+)/([A-Za-z0-9-]+)/?$ test.php?name=$1&id=$2&nick=$3

Your links should be:

test.php?name=john&id=2&nick=doe
share|improve this answer
add comment

Your "Method 2" looks appropriate to me to match http://www.testdomain.com/john/2/doe.

Only, you may want to try to add the RewriteBase / too, something like this:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/(.*)$ test.php?name=$1&id=$2&nick=$3 [L,QSA]

Instead of the base, you could try adding a slash at the start of the regex:

RewriteRule ^/([^/]+)/([^/]+)/(.*)$ test.php?name=$1&id=$2&nick=$3 [L,QSA]
#            ^--- this slash

You may also want to be careful with the QSA flag since other query string parameters will be kept in that way.

Note that rewriting to test.php as is means that this PHP program is in the same directory as your .htaccess file. If it is not there, then the rewrite may not take place because the mod_rewrite module detects the missing entry and thus bypasses the rule automatically. If that's the case, it should appear in your error logs. Otherwise you may need to put a path in front of the test.php script name.

RewriteRule ^/([^/]+)/([^/]+)/(.*)$ /test.php?name=$1&id=$2&nick=$3 [L,QSA]
#            ^--- those slashes  ---^

Note that we may need to get a copy of your access and error logs to see whether Apache actually tells you what's wrong. You may want/need to increase the verbosity to get more info.


P.S. This paragraph seems to be reversed... maybe you could edit your question and fix it?

I want to rewrite this URL http://www.testdomain.com/test?name=john&id=2&nick=doe to http://www.testdomain.com/test/john/2/doe or http://www.testdomain.com/john/2/doe.

share|improve this answer
add comment

You should just redirect everything to your script then process in there like so:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (.+) index.php?url=$1 [QSA]

So then you can explode the $_GET['url'] by / to get the individual parameters.

share|improve this answer
add comment

Use linux hosting. May be your hosting server is on windows. Windows gives problems sometimes with htaccess.

share|improve this answer
    
Can you provide links to sources that backs up your assertion that Windows hosts don't support htaccess well enough for this? As it stands, this is a very low quality answer. –  Stephen Ostermiller Mar 28 at 10:16
    
I had a problem like it. CHanged to linux and solved! –  Mehman Bashirov Mar 29 at 5:48
add comment

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.