Mobile app version of vmapp.org
Login or Join
Hamaas447

: Apache URL rewriting not working 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

@Hamaas447

Posted in: #Apache #Htaccess #UrlRewriting

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

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


I want to rewrite this URL www.testdomain.com/test?name=john&id=2&nick=doe to www.testdomain.com/test/john/2/doe.
test is the index file.

I tried these methods to rewrite:


Method 1:

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

Method 2:

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

Method 3:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /test?name=&id=&nick= [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?

10.05% popularity Vote Up Vote Down


Login to follow query

More posts by @Hamaas447

5 Comments

Sorted by latest first Latest Oldest Best

 

@Deb1703797

Your "Method 2" looks appropriate to me to match 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=&id=&nick= [L,QSA]


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

RewriteRule ^/([^/]+)/([^/]+)/(.*)$ test.php?name=&id=&nick= [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=&id=&nick= [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 www.testdomain.com/test?name=john&id=2&nick=doe to www.testdomain.com/test/john/2/doe or www.testdomain.com/john/2/doe.

10% popularity Vote Up Vote Down


 

@Candy875

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=&id=&nick=


Your links should be:

test.php?name=john&id=2&nick=doe

10% popularity Vote Up Vote Down


 

@Cugini213

You mentioned that two options are valid for you, so I'm using the second one, 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
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
www.example.com/test?name=john&id=2&nick=doe

into
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 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 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 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]

10% popularity Vote Up Vote Down


 

@Sarah324

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

10% popularity Vote Up Vote Down


 

@Karen161

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= [QSA]


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

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme