Conflicting rewrite rules htaccess - php

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.

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]

URL ShortCut Redirects

This is my .htaccess command for making URL shortcuts:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} .(.+)$
RewriteRule ^(.) ?open=encyclopedia&letter=$1&term=%1 [R,NC,L]
With the above works just fine:
example.com/Njumba ➛ example.com/?open=encyclopedia&letter=nj&term=njumba
Now, the problem is that it redirects merely one letter, but not two.
This is how I want it to be:
example.com/NJ ➛ example.com/?open=encyclopedia&letter=nj
Is this also achievable with .htaccess?
You can use this rule instead:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.{1,2}).*$ /?open=encyclopedia&letter=$1&term=$0 [R,NE,QSA,L]

htaccess skip part of the URL

I have a long URL like this http://example.com/interview/folder1/folder2/xxx. I need that page to be opened also in the short form like this http://example.com/interview/xxx.
I have tried this, it didn't help
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^interview/folder1/folder2/
RewriteRule (.*) interview/$1
Please note that this is an excerpt from the htaccess which is much larger.
What am I doing wrong?
You can try this .htaccess file:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^interview/folder1/folder2/
RewriteRule ^interview/(.*)$ /interview/folder1/folder2/$1 [L]

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.

Unable to create a beautiful url with HTACCESS

What I am trying to do is shorten this url:
example.com?controller=iphone&action=xyz
into:
example.com/iphone/xyz
This is what I tried which isn't working:
.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/$ index.php?controller=$1&action=$2 [L]
There's no obvious need to require the trailing slash in your pattern, nor limit it to finish immediately after with the line ending $ either. Note also I changed * to + because you'd almost certainly want both controller and action to be at least one character long.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+) index.php?controller=$1&action=$2 [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ index.php?controller=$1&action=$2 [L]

Categories