Getting rid of GET keys parameters in the URL, keep only values - php

I wanna to get rid of GET string parameters, from keys.
For example this is initial uri:
http://example.com/api/get_users_method?user_age_from=18&user_age_to=25&user_city=ohio
http://example.com/api/get_users_method?user_age_from=18&user_age_to=25
http://example.com/api/get_users_method?user_city=ohio
I wanna make like this:
http://example.com/api/get_users_method/18/25/ohio
http://example.com/api/get_users_method/18/25
http://example.com/api/get_users_method/ohio
How can i get this result? I found some solution, to edit htaccess file, but it doesn't work unfortunately, maybe i made some mistakes in htacces file? I'm using php. Thanks!
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/?$ /api/get_users_method?user_age_from=$1&user_age_to=$2&user_city=$3 [NC,L]

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ /api/get_users_method?user_age_from=$1&user_age_to=$2&user_city=$3 [NC,L]
You have to treat the rules separately. All Conditions preceding rules only apply to a single rule. Following rules are not touched by that rule.

You will 3 separate rules in your root .htaccess (a level above api directory)
Options -MultiViews
RewriteEngine On
RewriteRule ^(api/get_users_method)/(\w+)/(\w+)/(\w+)/?$ $1?user_age_from=$2&user_age_to=$3&user_city=$4 [NC,L,QSA]
RewriteRule ^(api/get_users_method)/(\w+)/(\w+)/?$ $1?user_age_from=$2&user_age_to=$3 [NC,L,QSA]
RewriteRule ^(api/get_users_method)/(\w+)/?$ $1?user_city=$2 [NC,L,QSA]

Related

Mod Rewrite lost all GET parameters [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 .

Rewrite .htaccess file but not working

I wish to rewrite this url:
http://salessurface.com/overseas/sub_page_details/MBBSAbroad/MBBS-in-UK/7
into:
http://salessurface.com/overseas/sub_page_details.php?sup_menu=MBBS%20Abroad&menu=MBBS-in-UK&main_menu_id=7
I tried adding the following to the .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ overseas/sub_page_details.php?sup_menu=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ overseas/index.php?sup_menu=$1&menu=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ overseas/index.php?
sup_menu=$1&menu=$2&main_menu_id=$3 [L]
but it is not working.
How do I accomplish that rewrite?
You don't need the conditions, you can do that with just a rule:
RewriteEngine on
RewriteRule ^overseas/sub_page_details/(.*)/(.*)/(.*) /overseas/sub_page_details.php?sup_menu=$1&menu=$2&main_menu_id=$3 [NC,L]
This will take the path after sub_page_details and put the first part in sup_menu, the second part in menu and third part in main_menu_id.
But I noted that in your sample URLs you use MBBS%20Abroad in the real URL but MBBSAbroad in the rewritten URL, if this is not a typo and the real URL has an space on it that doesn't have the rewritten URL then you need to use a less specific rewrite for that option:
RewriteEngine on
RewriteRule ^overseas/sub_page_details/MBBSAbroad/(.*)/(.*) /overseas/sub_page_details.php?sup_menu=MBBS\ Abroad&menu=$1&main_menu_id=$2 [NC,L]
This will hard code the sup_menu part of the URL and take the path after MBBSAbroad and put the first part in menu and second part in main_menu_id.

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

htaccess already stripping ".php" but need a sub page with url query

Here's what I have so far...
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ $1.php?%1 [NC,L,QSA]
So site.com/page works to view site.com/page.php.
What I want to add to this is on one particular page where there is a query string...
example: site.com/song.php?name=Song-Name -> I want it to be site.com/song/Song-Name.
I know there are a lot of post similar to this and I've looked through a ton but just don't understand quite how to accomplish this.
You will need a new rule to handle /song/ URIs:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^song/(.+)$ song.php?name=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ $1.php [L]
I have also corrected your 2nd rule since query string is already appended to target.

htaccess for rewrite URL is not working for my website

I have dynamic URL website (irasol.com) while i navigate to menu the url shows like
http://irasol.com/index.php?id=1
I want url like this
domainname/home
domainname/aboutus
domainname/contactus
domainname/apply
home, aboutus, contactus, apply are menu name it is already in database.
my htaccess file is
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]*)\.php$ /index.php?id=$1 [L]
Use this instead:
Options -Multiviews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/?$ index.php?id=$1 [B,L]
Explanation
The first three conditions make sure that domainname/aboutus is not a real file, so that we don't rewrite files that already exist.
Options -Multiviews removes a number of potential problems
In your current code, get rid of the .php in your pattern:
RewriteRule ^([^/]+)$ /index.php?id=$1 [L]
You are not matching .php extensions in the request. You are only routing matches to a query string on a real .php extension
As for a better solution:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?id=$1 [L]

Categories