Mod Rewrite lost all GET parameters [PHP] - php

Today i start with my mod rewrite for the php script.
Some words to the setup
I use a subdomain e.g. test.testsystem.com which points to root/test/
So i have some multiple GET Parameters for my PHP script like the following:
1) https://test.testsystem.com/index.php?a=foo
2) https://test.testsystem.com/index.php?a=foo&v=bar
3) https://test.testsystem.com/index.php?a=foo&c=testcode
so with mod rewrite i want to have
1) https://test.testsystem.com/foo
2) https://test.testsystem.com/foo/bar
3) https://test.testsystem.com/foo/c/testcode
What i currently have is
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteBase /test/
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?a=$1 [QSA]
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?a=$1&v=$2 [QSA]
For the first two URLs. Now it does no error 500 any more but the problem is i do not get any GET parameters in my php script.
Maybe someone can provide me here a working solution.
Regards

Assuming your test.domain.com is pointing to the /test folder , you can use something like the following in your /test/.htaccess .
Options -Multiviews
RewriteEngine on
#1)Rewrite /foo to /index.php?a=foo
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?a=$1 [L]
#2)rewrite /foo/bar to /index.php?a=foo&v=bar
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?a=$1&v=$2 [L]
#3)rewrite /foo/bar/buzz to /index.php?a=foo&v=bar&z=buzz
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?a=$1&v=$2&z=$3 [L]
The RewriteConds are important in each rules to avoid rewriting your existing files and directories .

Related

rewrite rule dont gives GET-Parameter

I have the following rule:
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)(?:/([^/]+))?/?$ index.php?a=$1&b=$2 [QSA]
This rule redirect all requests to my index.php and works like a charm. Now I would like to ADD another rule that redirect all requests for the directory /admin/ to admin.php in root. (A request for www.mysite.de/admin/login should end in www.mysite.de/admin.php?a=login&b=) The second parameter b should be optional.
How to reach this? Thx for any hint in advance.
You can test htaccess rules online (ex. here: htaccess.madewithlove.be)
This code works with:
www.mysite.de/admin/login
and
www.mysite.de/admin/login/123
edited
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^admin/?([^/]*)/?(.*)$ admin.php?a=$1&b=$2 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/?(.*)$ index.php?a=$1&b=$2 [NC,QSA,L]
#removed
#RewriteRule ^admin/([^/]+)(?:/([^/]+))?/?$ admin.php?a=$1&b=$2 [NC,QSA,L]
#RewriteRule ^([^/]+)(?:/([^/]+))?/?$ index.php?a=$1&b=$2 [QSA]
Notice that you need the L flag there. (RewriteRule Flags)
Also useful to see How L flags work - StackOverflow answer and Everything about mod rewrite - serverfault.com thread

mod-rewrite to work with oth server and userdir

I'm using Apache mod_rewrite and userdir.
I want to write a .htaccess that works everywhere, is there a way to remove the RewriteBase or set it up using Apache variable ?
Here is my .htaccess :
RewriteEngine On
RewriteBase /~leo/lstronic/v2/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . ./index.php [QSA,L]
I also need to keep the relative URL, that's how I do this :
$config['root_directory']=__DIR__;
$config['project_subdirectory']=str_replace("/home/leo/public_html/","/~leo/",$config['root_directory']);
//$config['project_subdirectory']=str_replace($_SERVER['DOCUMENT_ROOT'],"",$config['root_directory']);
Is there a way to make the two last lines working everywhere ?
Léo
You can use this rule to generate RewriteBase dynamically in an env variable and use it in your rules.
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . %{ENV:BASE}index.php [L]

two RewriteRule's for two different but like urls

My .htaccess is
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^api\/(.*)$ api.php?action=$1 [L,QSA]
RewriteRule . index.php [L]
so all requests i receive in my index.php and then parse query string, and one of my pages is "api documentation page" with url http://domain.com/api so i require page about api from templates
and another url is http://domain.com/api.php where i need receive $_GET['action'] and another data with $_POST
so it will work like http://domain.com/api.php?action=start, but i need call it like http://domain.com/api/start or http://domain.com/api/start/ and receive $_GET['action'] - "start"
but this string in my .htaccess doesnt work
RewriteRule ^api\/(.*)$ api.php?action=$1 [L,QSA]
You need to keep RewriteCond before every RewriteRule if you want to apply for all URLs or keep them in a separate rule. Try this .htaccess:
Options -MultiViews
RewriteEngine On
RewriteBase /
# ignore all files/directories from all rules below
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^api/(.+)$ api.php?action=$1 [L,NC,QSA]
RewriteRule . index.php [L]
Ah, I've had that problem before, can't remember exactly how it went, but this link seems relevant: https://wiki.apache.org/httpd/RewriteFlags/QSA
The only difference with yours is the slash at the beginning to make the paths absolute and you don't need to escape slashes. E.g.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/([a-z]*)/? api.php?action=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
But none of those differences seem to be the cause of the problem. As pointed out by nubhava, the RewriteCond's only affect the next rule though.
When faced with this kind of situation though, I usually end up using a router, to do the "rewriting" in PHP.
See also Mod-Rewrite or PHP router?
RewriteEngine On
RewriteBase /
RewriteRule ^api/([^/]*)$ /api.php?action=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
work's, http://htaccess.madewithlove.be/ help's me trying different combinations

Custom .htaccess Rewrites for individual files

I am making a music player that can use GET variables to select the play list, i would like to know how to use a single .htaccess file to keep my current .htaccess and only for music.php (or Ducke.Co/music) use a different rewrite to change music?f=PLAYLISTNAME to music/PLAYLISTNAME.
My current .htaccess is as follows:
ErrorDocument 500 /DuckeCo/ErrorPages/500
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]
Right under the RewriteBase / line, add:
RewriteCond %{THE_REQUEST} \ /+music\?f=([^[&\ ]+)
RewriteRule ^ /music/%1? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^music/(.+)$ /music?f=$1 [L,QSA]

Why my htaccess rules do not work perfectly?

I want to make 2 types of URL with these styles using htaccess and mod_rewrite.
http://www.site.com/product
http://www.site.com/product/1/something
For this, I've added these lines to the htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?p=$1&id=$2&des=$3 [N]
RewriteRule ^([^/]*)$ /index.php?p=$1 [F]
But whenever I run my application and hint http://localhost/project/, web server redirect me to http://localhost/.
Now I have 2 questions:
Why do I redirect to localhost and why the codes above do not work ?
How can I add a trailing slash to the end of clean URLs?
Adding
RewriteBase /
should help
Change the rules to
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ /index.php?p=$1&id=$2&des=$3 [N]
RewriteRule ^([^/]*)/?$ /index.php?p=$1 [F]
The final .htaccess should look like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ /index.php?p=$1&id=$2&des=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ /index.php?p=$1 [L]
You probably want to get rid of the N and F flags. Not sure why you are getting redirected, it doesn't look like the rules you have are responsible for that. You also want to repeat the 2 conditions you have for both rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?p=$1&id=$2&des=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /index.php?p=$1 [L,QSA]

Categories