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]
Related
Is there any possibility to rewrite url's with get variables globally, instead of specific files?
Changing
http://example.com/{something}?id=1
To:
http://example.com/{something}/id/1
Or without the word id:
http://example.com/{something}/1
This is what I got so far:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.phtml
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.phtml
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
With this I could remove the .phtml extension
I hope there is a specific answer for this!
I am assuming something to be a php file,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/id/([\d]+)$ $1.php?id=$2 [L] #with id in between
or
RewriteRule ^([\w-]+)/([\d]+)$ $1.php?id=$2 [L] #without id in between
You have to make sure rules are not conflicting.
This question already has an answer here:
htaccess mod rewrite with optional slash
(1 answer)
Closed 6 years ago.
I know this type of problem is asked a lot but I havent been able to find a resolution to this.
My .htaccess code rewrites it so that the first param is the page, then up to 3 after that are param1, param2, and param3. This works fine, for example when the url is "localhost/portal/dashboard" . But when the url is "localhost/portal/dashboard/" (The extra slash), it doesnt work and gives a 404 error.
Here is my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?page=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?page=$1¶m1=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?page=$1¶m1=$2¶m2=$3 [L]
Thanks in advance.
Untested, but adding an optional slash to the end of your rewrite matchers might work, like so:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?page=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?page=$1¶m1=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?page=$1¶m1=$2¶m2=$3 [L]
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.
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.
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]