Custom Parameter with HTACCESS - php

my URLs like this
http://example.com/mypage.php
http://example.com/{additional_parameter}/mypage.php
Here, my mypage.php gets {additional_parameter} and works further. My mypage.php work without {additional_parameter} also.
How to code this in htaccess?
Example: http://example.com/user_id/mypage.php will be rewrite to http://example.com/mypage.php?id=user_id

You can give this a try, I have tested it and its working fine.
Note: Here I am assuming your user_id can contain digits, alphabets or _
RewriteEngine on
RewriteCond %{REQUEST_URI} ^\/([\w]+)\/mypage\.php$
RewriteRule .* /mypage.php?id=%1 [L,END,QSA]
OR
RewriteEngine on
RewriteRule ^([\w\s]+)\/mypage\.php$ /mypage.php?id=$1 [L,END,QSA]
Update:
As OP's request url to work on either 0,1,2 or 3 parameters where he can handle its parameters sequence by his own.
RewriteRule ^(([^\/]*)\/)?(([^\/]*)\/)?(([^\/]*)\/)mypage\.php$ /mypage.php?id=$2&class_id=$4&subject_id=$6 [L,END,QSA]

Related

Custom URL in apache using get parameters

I'm trying to figure out how to rewrite urls using apache webserver and php.
The url below is the real nonrewritten url:
http://localhost:1337/rewritetest/index.php?id=12
And I want to reach it by
http://localhost:1337/rewritetest/index/12
My indexfile looks like this:
<?php
echo $_GET['id'];
?>
Is this possible? The "new" url doesn't include any parameter names so I guess I have to use an order of parameters instead but I dont know how to reach them in that case.
Below is as far I've come with my rewrite:
RewriteCond %{QUERY_STRING} id=([-a-zA-Z0-9_+]+)
RewriteRule ^/?index.php$ %1? [R=301,L]
RewriteRule ^/?([-a-zA-Z0-9_+]+)$ index.php?id=$1 [L]
Anyone have an idea of what I'm doing wrong?
it's located in the same folder as index.php
So, given the .htaccess file is located at /rewritetest/.htaccess (as opposed to the document root ie. /.htaccess) then...
RewriteRule ^/?([-a-zA-Z0-9_+]+)$ index.php?id=$1 [L]
If you request a URL of the form /rewritetest/index/12 then the above RewriteRule pattern won't actually match anything. It tries to match "index/12", but your pattern does not contain a slash so will fail. (Is the + inside the character class intentional?)
Try something like the following instead:
RewriteRule ^(index)/(\d+)$ $1.php?id=$2 [L]
This obviously specifically matches "index" in the URL. If you are always rewriting to index.php then you don't really need "index" in the URL - unless this means something different? This also assumes that the valuue of the id parameter consists only of digits.
To rewrite the more general .../<controller>/26 to .../<controller>.php?id=26 (as mentioned comments) then try something like:
RewriteRule ^([\w-]+)/(\d+)$ $1.php?id=$2 [L]
In per-directory .htaccess files the slash prefix is omitted on the URL-path that is matched by the RewriteRule pattern, so /? is not required. The above pattern also matches something for for the id, not anything. So, /index/ would not match.
If this is a new site then the "redirect" (from /index.php?id=12 back to /index/12) is not necessarily required. That's only really required if you are changing the URL structure on an existing site where old URLs already have inbound links and are indexed by search engines. In which case you could do something like the following before the internal rewrite:
RewriteBase /rewritetest/
RewriteRule %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^id=(\d+)
RewriteRule ^(index)\.php$ $1/%1 [R,L]
Or, for a more generic .../<controller>/26 to .../<controller>.php?id=26 (as above) then change the RewriteRule to:
RewriteRule ^([\w-]+)\.php$ $1/%1 [R,L]
The additional check against the REDIRECT_STATUS environment variable is to prevent a rewrite loop after having rewritten the URL to /index.php?id=12 earlier.

.htaccess Pretty URLs title in url

I'm trying to make pretty url but want to put content title in url instead id, then i put content title in url with query string:
index.php?action=content&id=22
changed to:
index.php?action=content&title=stack-over-flow
it's works fine. now i trying to make it pretty but got a problem in htaccess code.
before removing id in url code was:
RewriteRule ^([a-z]+)/([0-9]+)/?$ index.php?action=$1&id=$2 [NC,L,QSA]
then i changed to:
RewriteRule ^([a-z]+)/([a-z]+)/?$ content.php?action=$1&title=$2 [NC,L,QSA]
but it's not working and i will back 300 Multiple Choices page. well, i think it's a httaccess problem, but i'm new in htaccess, need a hand to fix this.
want this:
/content/stack-over-flow
Your regex is only allowing letters. It should also allow hyphen, numbers, upper case letters and underscore. Try this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]+)/([^/]+)/?$ content.php?action=$1&title=$2 [NC,L,QSA]

Rewrite automatically removes backslash if there's more than one?

I have a very simple url rewriting rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} !script.php
RewriteRule ^test/(.*) script.php?q=$1
The idea is to have this kind of urls: http://mywebsite.com/test/http://example.com
and then send http://example.com to the script.php as a query parameter. The problem is that I'm receiving http:/example.com instead of http://example.com. Also, http:////example.com would be sent as http:/example.com. What causes this behavior ?
Apache mod_rewrite engine converts multiple ///... into single / for pattern matching in RewriteRule directive. However if you match it using RewriteCond then you can match multiple /s.
You can use rule like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/+test/+(https?://.+)$ [NC]
RewriteRule ^ script.php?q=%1 [L,QSA]
The browser causes this behaviour. It contracts a sequence of / into 1 /, because it is still essentially a path. ///// does not change the directory we are in, so we could as well use /.
You have two options:
Change your links to use a query string instead. If you rewrite test/?q=something to script.php?q=something everything works as expected. You would do the following:
RewriteRule ^test/?$ script.php [L]
Since you don't alter the query string, the original query string is automatically copied to the new query string.
Don't make an assumption on how many slashes you will encounter. The url might not look correctly in the url bar of the browser, but if it is just a redirect, it will only be visible for a very short period of time.
RewriteRule ^test/(http|https):/+(.*)$ script.php?q=$1://$2

URL Rewrites not working

On my website I am trying to rewrite a long URL to a SEO friendly one.
I've got the following code, but it doesnt seem to affect anything! However if I type dgadgdfsg into my htaccess, it throws an internal server error. So I am presuming it is something with Rewrite Rule.
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /missing-people/user-profile.php?userID=$1&firstName=$2&lastName=$3 [L]
I have confirmed that mod_rewrite is on.
This is the current URL
http://mysite.com/missing-people/user-profile.php?userID=1&firstName=Liam&lastName=Gallagher
and this is what I want it too appear like
http://mysite.com/1/Liam/Gallagher
Change your RewriteRule to this (slightly modified from your version)
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ missing-people/user-profile.php?userID=$1&firstName=$2&lastName=$3 [QSA,L]
If that doesn't work try putting a R flag for testing purpose (which will make your browser change the original URI to: /missing-people/user-profile.php?userID=1&firstName=Liam&lastName=Gallagher
Presuming your userID is comprised only of digits and firstName and lastName are only alphanumeric.
RewriteEngine On
RewriteRule /(\d+)/(\w+)/(\w+)/ /missing-people/user-profile.php?userID=$1&firstName=$2&lastName=$3 [L]
A more strict version that does the same thing except it sets boundaries for the beginning and the end of the evaluated regex.
RewriteEngine On
RewriteRule /^(\d+)\/(\w+)\/(\w+)$/ /missing-people/user-profile.php?userID=$1&firstName=$2&lastName=$3 [L]

Want .htaccess to display / insteade of ? in URL and also i am able to get data like $_GET["param1"]

My Actual URL is below.
http://localhost/waterpump/index.php?param1=4498&param2=930&param3=876&param4=201&param5=vis
But my client want in below format.
http://localhost/waterpump/param1/4498/param2/930/param3/876/param4/201/param5/vis
And also i am able to get data using $_GET["param1"]
How can I do this through .htaccess?
Try examples from htaccess tricks
e.g.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^blog/([0-9]+)-([a-z]+) http://corz.org/blog/index.php?archive=$1-$2 [NC]
I am not sure if the number of parameters if fixed or variable but you would do essentially a matching part, where you match the url separated by slashes and then you would "rewrite" it to the original url
If you are set on using $_GET to get the parameters and there will always be between one and five parameters then you could use the following rewrite:
RewriteEngine On
RewriteRule ^waterpump/param1/(.*)/param2/(.*)/param3/(.*)/param4/(.*)/param5/(.*)/ /index.php?param1=$1&param2=$2&param3=$3&param4=$4&param5=$5 [nc]
RewriteRule ^waterpump/param1/(.*)/param2/(.*)/param3/(.*)/param4/(.*)/ /index.php?param1=$1&param2=$2&param3=$3&param4=$4 [nc]
RewriteRule ^waterpump/param1/(.*)/param2/(.*)/param3/(.*)/ /index.php?param1=$1&param2=$2&param3=$3
RewriteRule ^waterpump/param1/(.*)/param2/(.*)/ /index.php?param1=$1&param2=$2 [nc]
RewriteRule ^waterpump/param1/(.*)/ /index.php?param1=$1 [nc]
Editted
If you have implemented this correctly doing a print_r($_GET); on /waterpump/param1/x/param2/y/param3/z/ Should give you something like:
array (
'param1'=>x,
'param2'=>y,
'param3'=>z,
);
There are cleaner ways of doing this that would involve less rewrites, but some changes to your PHP. Also the method above will only work for waterpumps. Will there be anything other than water pumps?

Categories