.htaccess rewriting - php

I have this .htaccess that I've been using to rewrite URLs like these:
www.example.com/index.php?page=brand www.example.com/brand
www.example.com/index.php?page=contact www.example.com/contact
www.example.com/index.php?page=giveaways www.example.com/giveaways
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]
I used a file called index.php to handle the redirects. Code used below:
$page = trim($_GET['page']);
if($page == "giveaways")
require('pages/giveaways.php');
Now, I would like to add another URL type like these:
www.example.com/index.php?page=products&p=ford-mustang
TO
www.example.com/products/ford-mustang
How will I accomplish this? Any suggestion would be greatly appreciated.

You need to add a second RewriteRule above your current one.
Here's an example:
RewriteRule ^/(.*)/(.*)$ index.php?page=$1&p=$2 [L,QSA]
This will rewrite /product/ford-mustang to index.php?page=product&p=ford-mustang
Remember to add it above your current RewriteRule, because it first tries to match the first RewriteRule, when there's no match it will go on with the second RewriteRule and so further.

A URL rewrite for /products/ford-mustang would be:
RewriteRule ^(.*)/(.*)$ index.php?page=$1&p=$2 [L,QSA]

Related

.htaccess RewriteRule for category/page in custom CMS

I have a working RewriteRule that rewrites any page to the literal url:
RewriteRule ^(.+)$ index.php?page=$1 [QSA,L]
The problem is I want to add page=$1&category=$2 and convert it to...obviously... /category/page
I tried this, but it doesn't seem to work:
RewriteRule ^([^/]+)/([^/]+)$ index.php?page=$1&category=$2 [QSA,L]
I should note that I would still like to be able to access pages without categories - ie /login or /about that should go to index.php?page=about for instance
Your solution will be as below.
RewriteRule ^([0-9a-zA-Z_\-]+)/([0-9a-zA-Z_\-]+)$ index.php?page=$1&category=$2 [NC,L]
Input url : http://www.test.com/my-cat/my-page
and it will be treated as : http://www.test.com/index.php?page=my-cat&category=my-page
Try with below, we are instructing apache to don't look for directory or file.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?page=$1&category=$2 [QSA,L]
try this
#https://www.example.com/index.php?category=9&page=CBSE `#https://www.example.com/category/page/CBSE`
Method One:
#use this code for generate new url
RewriteRule ^index/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)$ index.php?category=$1&page=$2 [NC,L]
Method Two :
RewriteRule ^index/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)$ index.php?category=$1&page=$2
Note :Hit Your Url index.php?category=$1&page=$2 to convert $i & $2 Create Dynamic Url Your Id Bases
This will work for you.
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /index.php?category=$1&page=$2 [L]
If you want to generate rewrite urls easily, there are a lot online rewrite url generators.
Thanks for all the contributions...
Because I needed to cater for both /page and /category/page...I ended up doing this:
RewriteRule ^([0-9a-zA-Z_\-][^/]+)$ index.php?page=$1 [N]
RewriteRule ^([0-9a-zA-Z_\-]+)/([0-9a-zA-Z_\-]+)$ index.php?category=$1&page=$2 [L]
But that was only half the solution as I needed to modify my PHP to check whether category is set or not and to send a 404 if it doesn't match. Otherwise /invalid_category/valid_pagewould still work.
The ideas above all pushed me in the right direction, thanks.

Rewrite - 2 files with same parameters and located in same folder

I know that was a frequent question here in SO, but I can't rewrite some dynamics URL.
Hope someone can help me.
I've 2 files located in the same folder, that receive same number of parameters:
.com/serie.php?id=__123__&nombre=__string__
.com/episodio.php?idSerie=__123__&query=__1x1__
My desired pretty url are this:
for serie.php: .com/__123__/string
for episodio.php: .com/__123__/string/1x1-some-other-strings-not-relevants-here
I was able to make the first rewrite, like this:
Options +FollowSymLinks
RewriteEngine on
# Rewrite Serie.php
RewriteRule ^([^/]*)/$ /serie.php?id=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/$ /serie.php?id=$1&nombre=$2 [L]
Work fine, but the problem comes in the second rewrite.
The bold characters are "relevants" and I use this, to make some work.
I tried something like this:
RewriteRule ^([^/]*)-([^/]*)$ /episodio.php?idSerie=$1&query=$2 [L]
But I get 404 not found.
How can make this second rewrite?
Thanks in advice.
You can use:
Options +FollowSymLinks
RewriteEngine on
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/?$ serie.php?id=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ serie.php?id=$1&nombre=$2 [L,QSA]
RewriteRule ^([^/]+)/[^/]+/([^-]+)-[^/]*/?$ serie.php?id=$1&query=$2 [L,QSA]

Do url rewriting

I have url given below
http://localhost/main20/pages/view.php?id=hello-world
and want it to be shown as
http://localhost/main20/hello-world
I know its done using .htaccess and have found something here URL rewriting with PHP but its not fulfilling my requirement as my text "hello-world" will be changing on every request. Its something similar that wordpress does.
i found a solution my self here is the code and this http://roshanbh.com.np/2008/03/url-rewriting-examples-htaccess.html link solved my issue.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ pages/view.php?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ pages/view.php?id=$1
NOTE: I am assuming that in http://localhost/main20/ will have index.php if not then create one. It will look a like something http://localhost/main20/index.php.
Now your .htaccess file would be something.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [NC,L]
Options All -Indexes
In your index.php
<?php
define('WEBROOT','http://localhost/main20/');
//assuming your url is "http://localhost/main20/hello-wolrd"
$link=$_SERVER['REQUEST_URI'];
$linkArr=explode("/",str_replace(WEBROOT,"","http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']));
/*
print_r($linkArr);
will output
Array ( [0] => hello-wolrd )
*/
//now chech if $linkArr[0] is empty or not
if(!empty($linkArr[0]))
{
include("pages/view.php?id".$linkArr[0]);
}
?>
Hope this help you.
This is a good link for more information.
I think this .htaccess should work for you. Put it in the same folder where index.php is
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ pages/view.php?id=$1 [L,QSA,NC]

Simple mod rewrite query string but still failed

I want to create .htaccess mod rewrite but still have problem.
Suppose i created friendly URL like this :
mydomain.com/mypage/12345/title-of-news
I want that friendly URL process the below URL in hidden process :
mydomain.com/index.php?p=mypage&idnews=12345
Values of "p" and "idnews" are dynamic, so they will have different value.
I tried below code but still didn't work. Anything wrong?
RewriteBase /
RewriteCond %{QUERY_STRING} p=(\w+)&idnews=(\d+)
RewriteRule ^index.php /%1/%2 [L,R=301]
Any help would be appreciated. Sorry if this is duplicated question, if don't mind please tell me the answer link. Thanks a lot.
I think you don't need the RewriteCond you write there.
And you can use following rule
RewriteRule ^/?([0-9a-zA-Z_.-]+)/([0-9]+)/([0-9a-zA-Z_.-]+)$ index.php?p=$1&idnews=$2 [L]
If you want to change index.php?p=foo&idnews=bar to /foo/bar , try theses rules :
RewriteEngine on
##externally redirect "/index.php?p=foo&idnews=bar" to "/foo/bar"##
RewriteCond %{THE_REQUEST} /index\.php\?p=([^&]+)&idnews=([^&\s]+) [NC]
RewriteRule ^ /%1/%2? [L,R,NC]
##internally redirect "/foo/bar" to "/index.php?p=foo&idnews=bar"##
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?(.*)?$ /index.php?p=$1&idnews=$2 [NC,L]

2 different .htaccess rules

I am trying to set up for my website two different .htaccess rules, but I still cannot find the correct solution.
I would like to route everything on website.com/almost-everything - this is working me well. And further, I would like to add yet this route: website.com/car/car_id - and here comes troubles, I don't know how to set up it.
Here are my attempts:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?skill=$1 [L,QSA]
RewriteRule ^car/(.*)$ ./index.php?id=car&car_id=$1 # the wrong rule - the page with website.com/car/car_id just doesn't display the correct file
Could you help me please with the second rule?
Instead of
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?skill=$1 [L,QSA]
RewriteRule ^car/(.*)$ ./index.php?id=car&car_id=$1 # the wrong rule - the page with website.com/car/car_id just doesn't display the correct file
I would do this
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L] ## passthru + last rule because the file or directory exists. And stop all other rewrites. This will also help your css and images work properly.
RewriteRule ^car/(.*)$ /index\.php?id=car&car_id=$1 [L,QSA]
RewriteRule ^(.*)$ /index\.php?skill=$1 [L,QSA]
P.s. I seperated my rules with blank lines so it is clear how many there are. The above shows 3 distinct rules.
Rewrite works line by line, from the top to the bottom.
After checking the initial conditions (file doesn't exists), it encounters your first rule.
It says, if the URL is anything, modify it. It also has two options:
"QSA" means append the query string
"L" means this is the last rule, so stop processing
Because of this "L", it stops the processing, and nothing happens after this rule.
To fix this:
change the order of your rules, since "car/" is more specific
also add the L and QSA flags to the "car/" rule.
So:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^car/(.*)$ ./index.php?id=car&car_id=$1 [L,QSA]
RewriteRule ^(.*)$ index.php?skill=$1 [L,QSA]
A better solution would be, to redirect all your request to index.php, and then parse $_SERVER['REQUEST_URI']. Then you newer need to change htaccess for every new future.
In apache you can do that like this >
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php [L]
In php you can manualy fill your $_GET, so it would look like your old requests...
$f = explode('/', substr($_SERVER['REQUEST_URI'], 1));
switch ($f[0]) {
case 'car' :
$_GET['id'] = $f[0];
$_GET['car_id'] = $f[1];
break;
default:
$_GET['skill'] = $f[0];
}
# your old code, that reads info from $_GET
A better practice it would be to make class, that will take care of the urls.

Categories