.htaccess rewrite for sub pages - php

I have the current rewrite rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?page=$1 [L,QSA]
This works wonderfully for http://www.example.com/something but I would also like to be able to have sub pages, ie: http://www.example.com/something/something. I would like it to take the form of:
index.php?page=$1&subpage=$2
or
index.php?page=something&subpage=$1
Can someone please point me in the right direction?

Give this a go:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?page=$1&subpage=$2 [L,QSA]
You can also make both in one rule like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:([^/]+)|)(?:/([^/]+)|)/?$ index.php?page=$1&subpage=$2 [L,QSA]

Related

How to use get variables in htaccess (different order)

I am learning PHP and HTACCESS atm.
I have a problem where i use 4 different GET variables. x,y,z,w.
I need to create an URL structure which both can be.
mysite.com/x/y
but also
mysite.com/x/z
and different other combinations.
I
I have tried this in HTACCESS, but it only work if its the same kind of structure, and not with other combnations:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?x=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?x=$1&y=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?x=$1&y=$2&z=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)$ index.php?x=$1&y=$2&z=$3&w=$4 [L]
Specify [QSA] (Query string append) so you may pass a query string after your url.
Sample Code
RewriteEngine On
RewriteRule ^book/([^/]*)\.html$ book.php?title=$1 [QSA,L]

htaccess rewrite rule for subfolder (nice url)

I have trying to figure out how to get working in subfolder "admin" the same thing which works in root dir, problem is I don't know how to do that.
Currently I have:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ?arg=$1 [L,QSA]
# /
RewriteCond %{REQUEST_URI} !\.[[:alnum:]]+$
RewriteRule ^(.+[^/])$ /$1/ [R=301]
Now it works like:
http://example.com/page translate into http://example.com/?arg=page
What I would like to do in addition is this:
http://example.com/admin/page translate into http://example.com/admin/?arg=page
I don't know at all how to make that happen, can somebody help me please?
Thanks
You can use these rules with appropriate RewriteBase:
RewriteEngine On
RewriteBase /admin/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ?arg=$1 [L,QSA]
Or just add
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/admin/(.*)$ admin/index.php?arg=$1 [L,QSA]

More than one rewrite rule

I am trying to use like
www.site.com/somename
from two different pages. and i am using htaccess.
#1st condition
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) profile.php?username=$1 [QSA,L]
#2nd condition
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule (.+) clinicprofile.php?profilename=$1 [QSA,L]
It works if i only use for one of above page condition, But when i use both conditions 404 page is showing.
Please try to solve my problem.
Thanks to the suggestions,
what i have implemented is
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) profile.php?username=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^clinic/(.+)$ /clinicprofile.php?profilename=$1 [L]
clinic word need to added.

Conflicting rewrite rules htaccess

I have these two rules next to each other
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)?$ category-groups.php?furl=$1 [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)-([^/\.]+)?$ product.php?id=$2&head=$1 [QSA]
This works
/category-groups.php?furl=my-category-page
redirects to
/my-category-page
This doesn't work
/product.php?id=100&head=this-product
does not redirect to
/this-product-100
The product.php page is actually bounced to the category-groups.php page.
I hope that makes sense. I've tried many things but can't figure out to solve it
Thanks
You need to swap the order of your rules and make the regex match numbers:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)-([0-9]+)?$ product.php?id=$2&head=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)?$ category-groups.php?furl=$1 [QSA,L]
otherwise ^([^/\.]+)?$ will always just match everything.

.htaccess fake files and directorys

I am trying to use .htaccess to show /index.php?page=$1 when the file/directory doesn't exist
so example.com/Something
would not redirect to, but show example.com/index.php?page=Something
same thing for example.com/Something/SomethingElse
which should show example.com/index.php?page=Something/SomethingElse
Here is my current code, but it isn't working:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?page=$1 [L]
Any help would be much appreciated.
I use:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?path=$1&%{QUERY_STRING}
See if that works?

Categories