remove index.php from url with two parameters - php

I have the two following URL like below
www.site.com/index.php?key=blah
www.site.com/index.php?key=blah&pass=blahblah
I want to make it like the following using .htaccess
www.site.com/blah/blahblah
www.site.com/blah
in same webpage
How can i achieve this using .htaccess?

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ index.php?key=$1&pass=$2
should do it.
Edit: If you want the parameters optional, use this one.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /index.php?key=$1
RewriteRule ^([^/]+)/(([^/]+)?)$ /index.php?key=$1&pass=$2
I simply added a new rule where only one parameter is required. It does match first when only one parameter is given. If two parameters a given the second rule matches only.

Related

add " ? " in url via htaccess RewriteRule

i try use RewriteRule in htaccess
i want my url
site.com/f_search.php?langs=en&langs=en
works like
site.com/en/new.php?/en/f_search
i used this code in htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?/new.php+?+/([^/]*)/?/f_search/?$ f_search.php?langs=$1&langs=$2
but its not work with " ? "
if i change new.php+?+ to new.php+#+
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?/new.php+#+/([^/]*)/?/f_search/?$ f_search.php?langs=$1&langs=$2
it works good
site.com/en/new.php#/en/f_search
any idea to change # to ? in my url ???
thanks
The question mark and what follows is not part of the subject the pattern of a RewriteRule is matched against. That is clearly documented. Instead you need to use a RewriteCond to access the content of the query string.
I assume this is roughly what you are looking for, though you might have to tweak it to match your actual situation:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^/([a-z]+)/f_search$
RewriteRule ^/?(?:([a-z]+)/)?new\.php$ /f_search.php?langs=$1&langs=%1
From your question it is unclear where the two values for the argument "langs" in the rewrite target should get taken. So you may have to adjust that to your needs in the rule.
Also it is puzzling that you are trying to set that argument "langs" twice ... That makes little sense, the second attempt will simply overwrite the value of the first attempt when those values are handed over to your php based logic.
The documentation of the rewriting module for the apache http server offer a lot of precise help and good examples: https://httpd.apache.org/docs/current/mod/mod_rewrite.html
the final code
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([a-z]+)/new.php$ f_search.php?langs=$1 [L,NC]

Passing Different Parameter for Same File using URL Rewrite

I have user.php file and I m passing two parameters with it using URL Rewrite
but I m unable to use this rule as whichever rule is written first only gets executed and not the second one in case of my rule for userID rule doesn't work but work for webName. Any solution will be very much helpful to me.Thank you
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/(.*)$ user.php?webName=$1 [L,QSA]
RewriteRule ^user/(.*)$ user.php?userID=$1 [L,QSA]
Make a difference between two regural expression:
If you would like to catch user ID you should filter for only numbers: \d
Then if there is another chars in url not only numbers you can match for anything except kslash: [^/]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/(\d+)$ user.php?userID=$1 [L,QSA]
RewriteRule ^user/([^/]+)$ user.php?webName=$1 [L,QSA]

htaccess rewrite url error page and id

I want to achieve the following
Any links like mydomain.com/anyword should be rewritten to index.php?pg=$1
example:
mydomain.com/new -->index.php?pg=new
mydomain.com/purchased -->index.php?pg=purchased
mydomain.com/login -->index.php?pg=login
Any links like mydomain.com/anyword/anynumber should be rewritten to index.php?pg=$1&id=$2
example:
mydomain.com/new/12 -->index.php?pg=new&id=12
mydomain.com/purchased/240 -->index.php?pg=purchased&id=240
mydomain.com/login/10 -->index.php?pg=login&id=10
I have used the following htaccess code.
The first one works properly.
Regarding the second one, the entire parameter is passed to pg and there is no parameter id at all.
That is in case of url
mydomain.com/purchased/240
in my php $_Get has only 'pg' parameter and 'id' parameter at all
and pg=/purchased/240
Please suggest the proper way to do it.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?pg=$1 [NC,QSA]
RewriteRule ^([^/]+)/(\d+)/.*$ /index.php?pg=$1&id=$2 [NC,QSA]
Thanks
EDIT: Correct solution in case anyone made need it
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/([0-9]+) /index.php?pg=$1&id=$2 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?pg=$1 [NC,QSA]
This is because of the catch-all pattern. (.*) matches everything and rewrites it to the target. For your example, your first rule matches both requests, /foobar/ and /foobar/123 are rewritten to /index.php?pg=request . To fix this, you need to fix the order of your rules, you need to put your specific rule before the catch-all rule :
RewriteRule ^([^/]+)/(\d+)/?$ /index.php?pg=$1&id=$2 [L]
RewriteRule ((?!index.php).+) /index.php?pg=$1 [L]

htaccess rewrite parameters single page + iframe

I've been through plenty of docs but can't find the right rule on this case.
I have a single index.php file, containing a single iframe.php.
There is 3 optional parameters, although the first two have defaults if not specified. The parameters are passed to the iframe.
So I have
domain.com/?lang=en&country=us&game=myId
The iframe is called within the index like so :
<iframe src="iframe.php?lang=en&country=us&game=myId">
I would like the URL to be rewritten as :
domain.com/en-us/myId
without messing up the iframe parameters.
But
If the page is called without language parameters, the defaults are still set in the iframe, calling
domain.com
gives
<iframe src="iframe.php?lang=en&country=us">
In this case I would like the main URL to display
domain.com/en-us
Try something like this in the htaccess file in your document root:
RewriteEngine On
RewriteRule ^$ /en-us [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]{2})-([a-z]{2})(?:/(.*)|)$ /iframe.php?lang=$1&country=$2&game=$3 [L,QSA]
You then need to make sure you are constructing your URL's like:
domain.com/en-us/myId
and not use the query strings.
If you need to redirect the browser when accessing pages using query strings, include:
RewriteCond %{THE_REQUEST} \ /iframe.php\?lang=([a-z]{2})&country=([a-z]{2})(?:&([^&\ ]+))($|&|\ )
RewriteRule ^ /%1-%2/%3 [L,R=301]
To create separate rules for 2 and 3 parameters:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]{2})-([a-z]{2})/(.+)$ /iframe.php?lang=$1&country=$2&game=$3 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]{2})-([a-z]{2})/?$ /iframe.php?lang=$1&country=$2 [L,QSA]

Propper .htaccess rewrite mod

I am currently using .htaccess to rewrite my urls from
example.com/mixtape.php?id=(WHATEVER #)
...to...
example.com/m/(WHATEVER #)
The code i use to accomplish that is below:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m\/([^/\.]+)/?$ mixtape.php?mixid=$1 [L]
The next thing I am trying to achieve, in a clean matter, is to rewrite my
example.com/edit-mixtape.php?id=(WHATEVER #)
...to...
example.com/m/(WHATEVER #)/edit
EDIT: the "WHATEVER #" is the ID of the Mixtape I am editing. Normally i will use "$_GET['id']" to see what mixtape i'm referring to and then i fetch everything related to that number.
IS there anyone out there that would be able to help me successfully write a proper re-write mod?
Based on your comment, I would change your original rule as well to allow only for numbers:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m/(\d+)/edit/?$ edit-mixtape.php?mixid=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m/(\d+)/?$ mixtape.php?mixid=$1 [L]
By the way, I would probably use a general rule and write both urls to the same php file and handle the action in the controller.
You can use the same rules that you had before but just append an edit to the regular expression:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^m\/([^/\.]+)/edit/?$ edit-mixtape.php?mixid=$1 [L]
Note that your original rules have the query string mixid= while your examples say id=.

Categories