URL rewriting with two parameters using .htaccess and Apache - php

I have below code in .htaccess:
Options -Indexes
RewriteEngine on
RewriteRule ^category/([a-zA-Z0-9-/]+)/page/([0-9]+)/{0,1}$ themes/tema_2/category.php?slug=$1&page=$2 [NC,L,QSA]
I have problem with second parameter "page"... When I do $_GET['slug'] i got something like this: fudbal/page/2 if my url is like this: http://exmpl.com/category/fudbal/page/2.
So $_GET['slug'] returns all after category/ like it's slug.
I need to achieve this: When I echo $_GET['slug'] to echo fudbal and When I echo $_GET['page'] to echo 2.

You need to tweak your regex to fix your issue like this:
Options -Indexes
RewriteEngine on
RewriteRule ^category/([^/]+)/?$ themes/tema_2/category.php?slug=$1 [NC,L,QSA]
RewriteRule ^category/([^/]+)/page/(\d+)/?$ themes/tema_2/category.php?slug=$1&page=$2 [NC,L,QSA]

Related

htaccess redirect using different parameter name and(or) value

I have an page with multiple parameters. Is it possible to change the url's like below using htaccess or using php.
http://example.com/result.php?year=2012
Change To
http://example.com/year-2012
http://example.com/result.php?category=naturals
Change To
http://example.com/category-naturals
http://example.com/result.php?year=2012&category=naturals
Change To
http://example.com/year-2012/category-naturals
You can do it using .htaccess. Try something like this:
RewriteRule ^year-(.*) result.php?year=$1 [PT]
RewriteRule ^category-(.*) result.php?category=$1 [PT]
RewriteRule ^year-(.*)/category-(.*) result.php?year=$1&category=$2 [PT]
(http:\/\/.*\/).*?\?(?:(\w+)=(\w+)&?)*
Try this.Replace by $1$2-$3.See demo.
http://regex101.com/r/yA1jY6/2
You can have these rules in your root .htaccess:
RewriteRule ^year-([^/]+)/?$ result.php?year=$1 [NC,L,QSA]
RewriteRule ^category-([^/]+)/?$ result.php?category=$1 [NC,L,QSA]
RewriteRule ^year-([^/]+)/category-([^/]+)/?$ result.php?year=$1&category=$2 [NC,L,QSA]

URL RewriteRule .htaccess for more SEO friendly shape

I have URLs like
book.php?id=23 product.php?id=23 etc..
I want them to be something like
book/id/title
my current .htaccess is this
RewriteEngine On
RewriteRule ^book/([0-9]+)\-([a-z0-9_\-]+)/?$ book.php?id=$1 [NC,L,QSA]
but it's not working properly,
it's directing to book.php page despite of problems in page, I can't Get the Id parameter using $_GET , any help is appreciated
here's my url http://www.gamzesart.com/book.php?id=2
Actually I had this problem the other week, it turned out it was because the url reference was the same as the name as the php file I was calling, i.e:
RewriteRule ^book/([0-9]+)/([^/]+)/?$ book.php?id=$1 [NC,L,QSA]
Just as a test, change your rule to:
RewriteRule ^books/([0-9]+)/([^/]+)/?$ book.php?id=$1 [NC,L,QSA]
and try calling the URL as books/23 instead.
Try the following:
+FollowSymLinks # This is based on the comment of PeteR bellow in comments
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^book/([0-9]+)/([^/]+)/?$ book.php?id=$1 [NC,L,QSA]
</IfModule>
That works for me.
In my local PC I have try to access the URL : http://www.localSite.dch/book/12/nikos
and in my book.php file I have the following code:
<?php
echo "<pre>";
print_r($_REQUEST);
echo "</pre>";
?>
The result is this:
Array
(
[id] => 12
)

How to use htaccess code for URL rewrite in PHP?

I have a link like below:
In /country/search.php
<a href="<?php echo 'index.php?departments='.$value['department_id'].'&towns='.$value['town_id'].'&type='.$value['type_id'].'&id='.$value['id'] ?>">
When I use the below .htaccess code, it does nothing:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /country/index.php?departments=$1&towns=$2&type=$3&id=$4 [L]
It does in the tool:
http://example.com/0/0/4/122
... but when I click that link it shows old URL again.
What is the problem here?
I'm generating that .htaccess code using this tool: http://www.generateit.net/mod-rewrite/
Replace your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty URL
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(index\.php|)\?departments=([^\s&]*)&towns=([^\s&]*)&type=([^\s&]*)&id=([^\s&]*) [NC]
RewriteRule ^ %1/%2/%3/%4/%5? [R=301,L]
# internal forward from pretty URL to actual URL
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ /country/index.php?departments=$1&towns=$2&type=$3&id=$4 [L,QSA]
I think you misunderstood what the rewrite rules are trying to achieve in this context.
F.e. this rewrite rule
RewriteRule ^([^/]*)/([^/]*)$ /app.php?method=$1&arg=$2
Will ensure that a request to http://example.com/mymethod/myarg will be rewritten to
http://example.com/app.php?method=mymethod&arg=myarg
When you generate links from within your application you should know how you contstructed the rewrite rules and you have to build the link accordingly.
Original URL:
href="news.php?state="<?php echo $sql_res['state']?>"&country="<?php echo $sql_res['country']?>
Ideal URL:
/India/andhra%20Pradesh
And .htaccess code:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /news.php?country=$1&state=$2 [L]

.htaccess file not redirecting - guidance wanted

I have this url:
details
the url becomes in addressbar:
http://web228.sydney.webhoster.ag/soputnik/drive_to_london/?procid=12
but I want to process the logic in details.php.
How can I process the data in details.php in background and keep the first url in the address?
I tried this:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^drive_to_(.*)$ http://web228.sydney.webhoster.ag/soputnik/details.php [R=301,L]
but it is not working, error is: NOT FOUND
please help
Substitute your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^test/drive_to_(.*?)/?$ /test/details.php [L,NC]
If you don't want the URL to change, then it's not a redirect, just a rewrite.
Change:
RewriteRule ^/test/drive_to_(.*)$ /test/details.php [R=301,L]
To something like:
RewriteRule ^/test/drive_to_(.*)$ /test/details.php?city=$1 [L]
dont forget to pass any url params to details.php.
Rewriterule ^/drive_for_(.*)/\?(.*)$ http://domain.de/test/details.php/?$1 [R=301,L]
it seems relevant that you want to pass the city name as well, otherwise that context gets lost in the rewrite. If that is another url param you could pass it through to details.php as such
Rewriterule ^/drive_for_(.*)/\?(.*)$ http://domain.de/test/details.php/?$2&city=$1 [R=301,L]

.htaccess URL rewrite with one and two query string vars

I'm trying URL re-writing for the first time and I have the following URLS which need rewriting:
http://domain.com/resorts.php?countryname=Portugal
http://domain.com/rss.php?destination=Torremolinos&itemtype=rfamily
I've written the following in my .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule /countryname/(.*)/ resorts.php?countryname=$1
RewriteRule /destination/(.*)/itemtype/(.*)/ rss.php?destination=$1&itemtype=$2
But when I try the following, I keep getting URL not found. Please can someone point out what I may be doing wrong? Many thanks in advance.
http://domain.com/countryname/Portugal
http://domain.com/destination/Torremolinos/itemtype/rfamily
You can use this:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule countryname/(.*)/? resorts.php?countryname=$1 [NC,L]
RewriteRule destination/(.*)/itemtype/(.*)/? rss.php?destination=$1&itemtype=$2 [NC,L]

Categories