I am using below code in htaccess
RewriteRule ^/?service/abc/([\w-]+)/?$ xyz.php?name=$1 [L,QSA,NC]
RewriteRule ^/?service/pqr/([\w-]+)/?$ xyz.php?name=$1 [L,QSA,NC]
to display my URL something like:
http://example.com/service/abc/review
http://example.com/service/pqr/review
which is working for me. Now my issue is, I have to get abc or pqr a string from URL and display
I am trying to get the name like
$getURL=explode('/', $_SERVER["REQUEST_URI"]);// getting service/abc/review
echo $a = next($getURL); //it's displaying only service
Or is there any best way?
Woult you help me out ?
Use one rule, allow for either abc or pqr in that position, and append it as an additional GET parameter:
RewriteRule ^service/(abc|pqr)/([\w-]+)/?$ xyz.php?service=$1&name=$2 [L,QSA,NC]
And use $_GET['service'] to access the value then.
you can edit the htaccess rules in this way
RewriteRule ^/?service/(.*)/(.*)/?$ xyz.php?endpoint=$1&name=$2 [L,QSA,NC]
and retrieve them in xyz.php file
var_dump($_GET);
Related
We can get values from url like this.
abcd.com?id=123
We can get this id in php using
$id = $_get['id'];
But I see many urls like
abcd.com/123
How to do it and receive it in php page.
create a .htaccess file in webroot directory with
For numeric ID:-
RewriteEngine on
RewriteRule ^([0-9]+)$ index.php?id=$1 [L]
For alphanumeric ID:-
RewriteEngine on
RewriteRule ^([A-Za-z0-9]+)$ index.php?id=$1 [L]
Then, you can access domain.com/id as get parameter in index.php file
for that you have multiple choices :
Use a form and get params by POST,
Or you can use the PHP_REQUEST_URI by parsing it and get the member you want, for example if your URL is like http://mywebsite.com/users/125, in that php global variable, you will find an entry as an array that will contain members of your URL ( [0] -> ... [1] -> ... ).
Or, if you can use a Framework for examle Symfony, you will configure your routes and that's it.
Good Luck
For this page test.php
i have to use 2 format url
www.example.com/test/1234
and
www.example.com/test/1234/abcd
My .htaccess is
RewriteRule ^test/([^-]*)/([^-]*)$ /test.php?one=$1&two=$2 [L]
It's work good with www.example.com/test/1234/abcd but redirect internal error with www.example.com/test/1234
I want to know how can i edit .htaccess for work good with my 2 url format on test.php ?
Try the following instead:
RewriteRule ^test/([^-]*)$ /test.php?one=$1&two=$2 [L]
Just remove the 2nd /([^-]*) it is making no difference, as you can see here. Removing it will allow for you to access www.example.com/test/1234 too.
You can use:
RewriteRule ^test/([^/-]+)(?:/([^-]*))?$ /test.php?one=$1&two=$2 [L]
I have page redirect URL like below with more than one parameters.
http://example.com/search.php?layout=details&Keyword=mobile&view=product&CategoryId=1026&product_id=93477
From that URL i'm taking all parameter values to take a data from database.
So when i load this page i need to show URL most user friendly like below. In that URL should contain only the value of product_id which is i'm using my redirect URL
http://example.com/93477.htm
I have tried to rewrite this URL like below. But in that i couldn't use the parameters "categoryId" and "keyword"
rewrite ^/([a-zA-Z0-9/-]+).htm /search.php?layout=details&view=product&Keyword=$3&CategoryId=$2&product_id=$1 last;
Please anyone help me to done it successfully..
Regards,
VIGNESH KUMAR K
Try this mate ,
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/search\.php\?layout=details&Keyword=([^&]*)&view=product&CategoryId=([^&]*)&product_id=([^&]*) [NC]
RewriteRule ^ /%2/%3/%1? [L,NE,R=302]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /search.php?layout=details&Keyword=$2&view=product&CategoryId=$3&product_id=$1 [L,QSA]
The original URL:
http://example.com/search.php?layout=details&Keyword=mobile&view=product&CategoryId=1026&product_id=93477
The rewritten URL:
http://example.com/93477.htm
Update 1 :
[QSA] has been added in the above code now so that to append the query string in order to retrive
dynamic parameters using PHP $_GET
http://httpd.apache.org/docs/current/rewrite/flags.html#flag_qsa
Update 2:
Edited the .htaccess code to make Keyword and CategoryId Dynamic
So I'm currently using my .htaccess to write my URL to be more URL friendly.
But I'm having trouble getting the fourth variable that could be more folders. I'll try and explain it as best I can below.
So at the moment I have the following URL working:
www.mysite.co.uk/first/second/third
Now for this I am using the rewrite rule:
RewriteRule ^e/([a-zA-Z0-9-/]*)/([a-zA-Z0-9-/]*)/([a-zA-Z0-9\-\/]*)$ index.php?var_1=$1&var_2=$2&var_3=$3 [L]
My problem comes when I one of the following happens:
www.mysite.co.uk/first/second/third/fourth
www.mysite.co.uk/first/second/third/fourth/fifth
www.mysite.co.uk/first/second/third/fourth/fifth/sixth
From the above I want the following variables to be returned:
$_GET['var_1'] = first
$_GET['var_2'] = second
$_GET['var_3'] = third/fourth
OR
$_GET['var_3'] = third/fourth/fifth
OR
$_GET['var_3'] = third/fourth/fifth/sixth
So basically I always have a 'var_1' and 'var_2' set but the third variable can be the rest of the URL.
I hope this makes sense and that it's possible.
I'm not sure why you your rule starts with ^e/
if your urls will be in format : www.mysite.co.uk/e/first/second/third/fourth
then use this regex:
RewriteRule ^e/([a-zA-Z0-9-]+)(/([a-zA-Z0-9-]+))?(/([a-zA-Z0-9-/]*))?$ index.php?var_1=$1&var_2=$3&var_3=$5 [L]
If you need it without /e/ at start www.mysite.co.uk/e/first/second/third/fourth
then remove /e/ from start
RewriteRule ^([a-zA-Z0-9-]+)(/([a-zA-Z0-9-]+))?(/([a-zA-Z0-9-/]*))?$ index.php?var_1=$1&var_2=$3&var_3=$5 [L]
Example above will work even with url-s like /first and /first/second but if you don't need it and you want to have this rule just in case of 3 or more params then you can simplify rule:
RewriteRule ^([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-/]+)$ index.php?var_1=$1&var_2=$2&var_3=$3 [L]
i'm replacing this
localhost/load.php?code=foo
with this
localhost/foo
using this htaccess code
RewriteRule ^(\w+)$ ./load.php?code=$1
works pretty good! but what about including other get tag in the same URL like
localhost/load.php?user=foo&code=foo
replaced it with htaccess to be like
RewriteRule ^(\w+)$ ./load.php?user=$1&code=$1
but doesn't work as well! so what is the correct htaccess code to do this?
Depending on your other RewriteRules you might want to use a catch-all rule with the QSA (QueryStringAppend) flag.
The QueryString is the part after ? so user=foo&code=foo.
If you have a rule that says:
RewriteRule ^(.*)$ load.php?__path=$1 [QSA]
And you call:
my.domain.com/page/sub?foo=bar&baz=ipsum
load.php will get the following GET:
__path = page/sub
foo = bar
baz = ipsum
With a rule like this you can handle any URL.