Get variable and htaccess not working - php

I am using htaccess to generate the following url
http:/localhost/classifieds/9/my-classified-title-goes-here
using
RewriteRule ^aggelies/([0-9]+)/(.*)$ aggelies.php?id=$1&category=$2 [NC]
But in the specific page i also need to have pagination in the form of
http:/localhost/9/classifieds/my-classified-title-goes-here/?page=2
but when i cannot pass the page number in the code.
$page_number = (isset($_GET['page']))? $_GET['page'] : '1';
echo $page_number;
seems to not work

You need to use the QSA (QueryStringAppend) flag
RewriteRule ^aggelies/([0-9]+)/(.*)$ aggelies.php?id=$1&category=$2 [NC,QSA]

Related

I want to make my long reference link dynamic with htaccess

First of all, I have a link that takes 2 parameters;
http://localhost/project/home.php?SK=2&referance=1
?SK=2 is my first parameter and &referance=1 is my second parameter. The SK=2 parameter draws my files in the php file, and the referance=1 parameter prints the value from the get method to my registration page. What I want is to put this link in below format with htaccess
http://localhost/project/register?referance=1
The link I made before brings my register page, but now I want it to bring the reference code, but unfortunately it doesn't.
Htaccess code I used before;
RewriteRule ^register$ home.php?SK=2 [NC,L]
RewriteRule ^register$ home.php?SK=2 [NC,L]
You just need to add the QSA (Query String Append) flag to your existing rule.
For example:
RewriteRule ^register$ home.php?SK=2 [QSA,NC,L]
This will now rewrite /register to /home.php?SK=2 and /register?referance=1 to /home.php?SH=2&referance=1.
Reference:
http://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_qsa

Change url using htaccess wont work

Hi I am trying to change my url using htaccess but it didn't work anymore.
http://localhost:8888/cPanel/abc?page=general-settings
RewriteRule ^cPanel/([\w-]+)/?$ abc.php?page=$1 [L,QSA]
What i am doing wrong anyone can help me here please ?
I want to change the url like this:
http://localhost:8888/cPanel/general-settings
<?php
$page ='';
if($_GET['page']){
$page = $_GET['page'];
if($page == 'general-settings'){
include "/pages/general-settings.php" ;
}
}
?>
The error stands in the rule. Your actual rule is:
RewriteRule ^cPanel/([\w-]+)/?$ abc.php?page=$1 [L,QSA]
which is missing cPanel, if you want to achieve http://localhost:8888/cPanel/general-settings as result
With this rule (which means: when you Apache match cPanel/*anything*, hit the resource at cPanel/abc.php?page=*anything*) it should work:
RewriteRule ^cPanel/(.*)$ cPanel/abc.php?page=$1 [L,QSA]
Test this code
RewriteEngine on
Options +FollowSymlinks
RewriteRule ^cPanel/(.+)/?$ abc.php?page=$1
url: http://localhost:8888/cPanel/general-settings

htaccess rewrite url rule trouble detect $_GET isset

RewriteEngine On
RewriteRule ^about/([^/.]+)/?$ about.php?id=$1 [L,QSA]
I have a page use htaccess rewrite rule for url
/about/33
the page require $_GET['id'] to fetch db
however i have trouble to detect GET variable isset
if(isset($_GET['id'])){fetch data...}else{header("location:...");}
if user only enter /about/ without $GET['id'] the page will not found instead run header("location");
is anyway to solve this?
try this:
RewriteRule ^about/([^/.]*)/?$ about.php?id=$1 [L,QSA]
Put * instead of +

Get Page Number from Rewrited URL

i wrote this Rewrite Rule :
RewriteRule ^(products)/(page[0-9]+)?/?$ index.php?action=$1&page=$2 [L]
in following url this will happen :
rewrited url : http://www.silvergroup.ir/products/page2
what actually is : http://www.silvergroup.ir/index.php?action=products&page=page2
but i need the number of page only and whole of page variable is optional, this means following url is allowed :
http://www.silvergroup.ir/products
thank you
Use this rule instead:
RewriteRule ^(products)(?:/page([0-9]+))?/?$ index.php?action=$1&page=$2 [L,NC,QSA]
This will forward
/products to /index.php?action=$1&page=
/products/ to /index.php?action=$1&page=
/products/page2 to /index.php?action=$1&page=2

How to get values from query string and pass it to new page using .htaccess file

currently I m using following code in my site in .htaccess file :
RewriteRule ^([^/\.]+).php?$ comman.php?cat=$1 [L]
This redirects user to comman.php page say, user requests
http://www.mysite.com/myfolder/page1.php
will redirects to
http://www.mysite.com/myfolder/comman.php?cat=page1
This works fine. My question is how can I achieve following
http://www.mysite.com/myfolder/page1.php?var1=123
to redirect
http://www.mysite.com/myfolder/comman.php?cat=page1&param=123
i.e. whatever value passed to url using get method to add in my new url.
Thanks in Advance.....
You should add the QSA flag:
RewriteRule ^([^/\.]+).php?$ comman.php?cat=$1 [L,QSA]
QSA stands for Query String Append. Anything after the ? in the original URL will be appended to the rewritten URL.
You need to ass QSA to your rule, i.e.
RewriteRule ^(.*)$ /index.php?pageid=$1 [QSA,L]
You can use the QSA flag in your RewriteRule for that.
See the docs.

Categories