htaccess With a different amount of variables - php

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 ..

Related

redirecting a php url with 2 parameter

I am trying to set up my api to have 2 endpoints, and the simplest way I have seen to do this is to have a .htaccess file that would redirect something like www.website.com/api/category/platform to display the same as www.website.com/api/index.php?category=platform . The issue I am now running into is that I want it to include a second parameter such as www.website.com/api/index.php?category=platform&filter=price to be the same as www.website.com/api/category/platform/price or even www.website.com/api/platform/price
The current .htaccess file is
RewriteEngine On
RewriteRule ^/?category/([^/d]+)/?$ index.php?category=$1 [L,QSA]
I figured it out and I was able to use something along the lines of
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ project3/api/index.php?category=$1&filter=$2 [L]
You can use smthing like this.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?category$ $1category/ [R=301,L]
RewriteRule ^ - [L]
RewriteRule . index.php [L]

.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.

htaccess need name in front of domain

So i have a file named page.php, which has 2 get variables. the u and edit so at the moment i have
mysite.com/page.php?u=dan &
mysite.com/page.php?u=dan&edit
But how can i make the url look like this using htaccess?
dan.mysite.com &
dan.mysite.com/edit
At the moment i have tried this
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ page.php?u=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ page.php?u=$1
but this just makes
mysite.com/dan
If I understand your requirement correctly, you can use:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ page.php?u=dan&$1 [L,QSA]

Mod Rewrite and Arabic characters

I currently have the following rules in my .htaccess file that work perfectly for the English site.
RewriteEngine On
RewriteRule ^index.html$ index.php [L]
RewriteRule ^/?([0-9a-zA-Z_-]+)/?$ rewrite.php?param1=$1 [L]
RewriteRule ^/?([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+)/?$ rewrite.php?param1=$1&param2=$2 [L]
RewriteRule ^/?([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+)/([^/]+)/?$ rewrite.php?param1=$1&param2=$2&param3=$3 [L]
But, on the Arabic website, this throws a "404" error.
Examples that work:
http://www.mysitedomain.xyz/work/website/pages/goals-and-objectives
http://www.mysitedomain.xyz/work/website/solutions/banking
Examples that DO NOT work:
http://www.mysitedomain.xyz/work/website/pages/الاهداف
http://www.mysitedomain.xyz/work/website/solutions/بنوك
Any idea how to fix that?
Thanks
Given your URL layouts, I don't think you really need to be so specific. You probably just want to split on slashes:
RewriteEngine On
RewriteRule ^index.html$ index.php [L]
RewriteRule ^/?([^/]+)/?$ rewrite.php?param1=$1 [L]
RewriteRule ^/?([^/]+)/([^/]+)/?$ rewrite.php?param1=$1&param2=$2 [L]
RewriteRule ^/?([^/]+)/([^/]+)/([^/]+)/?$ rewrite.php?param1=$1&param2=$2&param3=$3 [L]
in place of ([0-9a-zA-Z_-]+) this utilize (.*), for any content

.htaccess short and friendly url

I'm looking for the right .htaccess rule to make a Facebook.com/user URL like
What I want if I access for example
www.domain.com/StackOverFlow
It will be rewrite to
www.domain.com/index.php?category=StackOverFlow
Or
www.domain.com/category.php?item=StackOverFlow
Any help? I've tried some question here, but they doesn't explicit do this behavior.
Thanks in advance
Edited:
Forgot to say that I only want to be redirected using this rule, anything that after the domain, doesn't have any extension
For example it should redirect www.domain.com/categoryDescription but no www.domain.com/categoryDescription.php or www.domain.com/categoryDescription.html
The example below takes the URI after .com/ and assigns that to $1.
You can do the same for "item"
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?category=$1 [L]
<IfModule mod_rewrite.c>
RewriteEngine on
# Rewrite URLs of the form 'x' to the form 'index.php?category=x'.
RewriteRule ^(.*)$ index.php?category=$1 [L,QSA]
</IfModule>
Try:
RewriteRule ^StackOverFlow$ index.php?category=StackOverFlow [NC,L]
or:
RewriteRule ^StackOverFlow$ category.php?item=StackOverFlow [NC,L]
In generally:
RewriteRule ^([^/]*)$ index.php?category=$1 [NC,L]
or:
RewriteRule ^([^/]*)$ category.php?item=$1 [NC,L]

Categories