Question

Can someone help me again regarding this rewrite module,

with this rules

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) $1.php [QSA,L] 

with this rules, i can get the requested url www.example.com/test to www.example.com/test.php but how can i get following url www.example.com/test/test2/test3/test4 to www.example.com/test.php

i tried

RewriteRule (.*)/?$ $1.php [QSA,L]

don't laugh at me... i actually tried

RewriteRule (.*)/(.*)/(.*)/(.*)$ $1.php [QSA,L] 

and this works for this link

www.example.com/test/test2/test3/test4 to www.example.com/test.php

but, not for this one...

www.example.com/test to www.example.com/test.php

i'm using explode on Php cause i don't know how many parameters, that i need, if it is only 2 parameters, and this rules should be work.

RewriteRule ^(.+/)?([^/]*)$ p.php?s=%1&p=$2 [QSA,L,NC]

i did keep checking every similiar questions, which appear on the right, but i don't see any solution yet.

Was it helpful?

Solution

You were creating an endless redirect. Try this checking for .php in the request. Also if you're not pulling the slashes the Apache would redirect to a non-existent index.php inside of some folder.

Example 1

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/ $1.php [QSA,L]

This takes the first directory matched and pulls it into the query string.

www.example.com/file/param1/param2/param3 shows this www.example.com/file.php

Example 2

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

This takes the first directory matched and pulls it into the query string. The rest of the URI is added as a parameter

www.example.com/file/param1/param2/param3 shows this www.example.com/file.php?params=param1/param2/param3

Example 3

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?([^/.]+)?/?([^/.]+)?/?([^/.]+)?$ $1.php?p1=$2&p2=$3&p3=$4 [QSA,L]

www.example.com/file/param1/param2/param3 shows this www.example.com/file.php?p1=param1&p2=param2&p3=param3


Note: [QSA] keeps the existing parameters, so &original_parameter=param is appended to the rewritten query string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top