.htaccess RewriteRule for category/page in custom CMS - php

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.

Related

mod_rewrite: form URL with multiple parameters to a shorter one (without middleparts)

Right now I work with these type of URLs:
http://subdomain.example.com/index.php?page=contact
http://subdomain.example.com/index.php?page=about
http://subdomain.example.com/index.php?page=category&category_id=1
http://subdomain.example.com/index.php?page=item&item_id=1
And I want them to look like this:
http://subdomain.example.com/contact/
http://subdomain.example.com/about/
http://subdomain.example.com/category-1/
http://subdomain.example.com/item-1/
So, I accommodate my page/php file into the following:
category_id -> category_name
item_id -> item_name
And my .htaccess file to this:
RewriteEngine on
RewriteRule ^([^/]*)/$ /index.php?page=$1 [L]
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?page=$1&category_name=$2 [L]
Which gives me some nice URLs but I want them without the middlepart. In other words I want to get rid of "category" and "item" for example.
http://subdomain.example.com/category/category-1/ -> http://subdomain.example.com/category-1/
When I try and change my .htaccess to the following I get this to work. BUT you can see that I had to comment out the 2nd line and so my contact page doens't work any longer :(
RewriteEngine on
#RewriteRule ^([^/]*)/$ /index.php?page=$1 [L]
RewriteRule ^([^/]*)/$ /index.php?page=category&category_name=$1 [L]
I know it has something to do with the parameters, one against two but I can't figure out which way I have to follow to get this to work which makes me to have fit.
I really would appreaciate if somebody can help me out solving this!
If I understand...
RewriteEngine on
# Not for real file or directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# for category-1/ and all like xxxxx-xxx-nnn
RewriteRule ^([^/]+-\d+)/?$ /index.php?page=category&category_name=$1 [L]
# for contact/ or about/ or other/
RewriteRule ^([^/]+)/$ /index.php?page=$1 [L]
The result :
http://subdomain.example.com/lord-of-the-rings-1/
-> http://subdomain.example.com/index.php?page=category&category_name=lord-of-the-rings-1

htaccess With a different amount of variables

I have a website that on one specific page it requires an extra variable.
At the moment, the htaccess I am using is:
RewriteEngine On
RewriteRule ^([^/]*)/$ /index.php?page=$1 [L]
Which works fine with one variable.
How would I get it to work with :
http://tessite.com/index.php?page=test&id=1
So it looked like:
http://tessite.com/test/1
Thanks for any help or replies.
It can be coded like this:
RewriteRule ^([^/]*)/([^/]*)/$ index.php?page=$1&id=$2 [L]
RewriteEngine On
RewriteRule ^(.*)/(.*)/$ /index.php?page=$1&id=$2 [L]
RewriteRule ^(.*)/$ /index.php?page=$1 [L]
Just add $2!
Also, [^/] isn't required, you can simply use ..

How to do profile pages like: /1234? (No "#" and no "?id=")

I want to do profile pages in my site. Right now are identified with a hash such as:
www.mysite.com/#123123
However I noticed that sites like Facebook or Twitter use URLs such as: facebook.com/1234 to identify profiles.
How can I do this on my site?
With mod_rewrite, but depending on your controller, something like:
RewriteEngine On
##Do rewrite if NOT file
RewriteCond %{REQUEST_FILENAME} !-f
##Do rewrite if NOT dir/folder
RewriteCond %{REQUEST_FILENAME} !-d
##Numeric
RewriteRule ^([0-9]+)$ index.php?id=$1 [L]
##Or for AlphaNumeric (any case)
RewriteRule ^([a-zA-Z0-9]+)$ index.php?id=$1 [L]
##Or for Anything after first slash
RewriteRule ^(.*)$ index.php?route=$1 [L]
##Note using this method you would need a (Router) controller that splits
## the request.
##Eg site.com/id/Af13s passes index.php?route=id/Af13s then with
## explode('/',$_GET['route']) you can get the action/sub action
##or have multiple entry's for each section
RewriteRule ^id/([a-zA-Z0-9]+)$ index.php?id=$1 [L]
RewriteRule ^page/([a-zA-Z0-9_]+)$ index.php?page=$1 [L]
RewriteRule ^admin/([a-zA-Z0-9_]+)$ admin.php?route=$1 [L]
Hope it helps
It's likely they are using .htaccess or similar to redirect the /1234 to some script like profile.php?id=1234
To answer your question better, look into mod_rewrite. Also, this might be of help, particularly the "Create beautiful URLs" section.

.htaccess rewriting

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]

Rewriting example.com/section.php?username=xyz to example.com/xyz/section

Using the following htaccess, I have been able to rewrite example.com/profile.php?username=xyz to example.com/xyz,
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?user=$1
Adding the following to the above,
RewriteRule ^([a-zA-Z0-9_-]+)/section$ section.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/section/$ section.php?user=$1
did not resolve example.com/section.php?username=xyz to example.com/xyz/section.
Am I doing something wrong here?
First of all: The manner of speaking would rather be the opposite: The rules you showed are to rewrite requests like /xyz internally to /profile.php?username=xyz and not vice versa.
Now if you want to rewrite requests like /xyz/section internally to /section.php?username=xyz where section and xyz are variable, try these rules:
RewriteRule ^([a-zA-Z0-9_-]+)/?$ profile.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([^/]+)/?$ $2.php?user=$1
To look for static files (images, css) in the right directory without having to write the file address, do:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Before writing the code suggested by RageD
(Sorry, should have posted it as a comment but I needed newlines)
I tested it and it works. Try this:
RewriteRule ^([a-zA-Z0-9_-]+)\/section$ section.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)\/section\/$ section.php?user=$1
All I did was escaping the / in the URL since / is a regex delimiter.

Categories