Rewrite rule with two $_GET - .htaccess

My .htaccess file is:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /art/art.php?galerie=$1
So thanks to this I can access the different galeries that exist :
website.com/art/sun or website.com/art/beach for example ( as long as the galerie exists otherwise returns a error message saying page doesn't exists)
This all works fine up to here .
But i needed to add a $_Get on the page also :
website.com/sun?row=2 for example.
But i cannot do this as sun?row=2 isn't a registered galerie ...
How do I get around this problem ?

You can use the QSA (Query String Append) flag to forword new query strings to the target :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /art/art.php?galerie=$1 [QSA]
Now This will rewrite
/foo?querystring
to
/art/art.php?galerie=foo&querystring

Related

Replace items with RewriteRule using .htaccess

I'm trying to use the Url rewrite using "RewriteRule".
I would use this link to show a new article in my blog:
https://www.example.com/article/how-to-visit-italy-in-winter
The original script (including vars) is
https://www.example.com/post.php?idpost=how-to-visit-italy-in-winter
This is my .htaccess
RewriteEngine on
RewriteRule ^article/$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .$ https://www.example.com/post.php?idpost=$1 [L]
but it doesn't work, the browser redirect to:
https://www.example.com/post.php?idpost=
I'm trying to resolve about 2 days witout success :(
Thank you.
You rule looks fine except for two things . Your $1 back reference is undefined and empty as you are not capturing any URI. To capture parts of the uri you need to use Regex capture-groups (.*) .
2) You need to use an absolute path instead of the full url as your RewriteRule`s destination if you want to silently redirect the url.
RewriteEngine on
RewriteRule ^article/$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule article/(.+) /post.php?idpost=$1 [L]

Getting multiple variables with Mod Rewrite

This my .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^products/([^/\.]+)/?$ /product.php?$1
RewriteRule ^category/([^/\.]+)/?$ /category.php?cat=$1&page=$2 [QSA,L]
the link i put into the browser is site.com/category/batteries/1/ where batteries is category and 1 would be the page. However, my site only gets the category (so site.com/category/batteries/ works, but site.com/category/batteries/1/ returns a 404.)
category.php gets the value being passed in cat, but not the value in page. I keep reading on this, but just can't understand where I'm going wrong.
You're missing a capturing group so $2 is never set. Try:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^products/(.*+)/?$ product.php?$1
RewriteRule ^category/(.*+)/(\d+)/?$ category.php?cat=$1&page=$2 [QSA,L]
Changing the regex worked. Just getting the text inbetween slashes instead of trying to find the digit gave me the desired result.
RewriteRule ^category/([^/]+)/([^/]+)/? /category.php?cat=$1&page=$2 [QSA,L]

Giving parameters to php with RewriteRule

I need a RewriteRule to call a php file with parameters specified in the URL entred
ex :
if the url entered
http://www.example.com/folder1/sub1
to make it call go.php?p1=folder1&p2=sub1
Try using
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)?/(.*) /go.php?p1=$1&p2=$2
Where RewriteCond %{REQUEST_FILENAME} !-f,!-d tell mod_rewrite to apply the rule just to paths not mapped to existing files and directories.
See also the official documentation.

Rewrite Rule Loop. How to stop applying rules

I have an .htaccess file redirecting blog post urls like /blog/2012/11/30/this-post to /blog/post.php?id=this-post. Works fine until I apply a second rule below it that is also a match. This rule is set to take a path formatted url like /this/is/a/pageid and redirect to /page.php?id=pageid. It doesn't care how long the path is, it just uses the last directory in the path as the id. Unfortunately, this rule matches everything and I'm not sure how to stop the redirect after the first match. Here is my .htacess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/([0-9]+)/([0-9]+)/([0-9]+)/([A-Za-z0-9-]+)$ /blog/post.php?id=$4 [L]
RewriteRule ([^/]+)/?$ /page.php?id=$1 [L]
Thanks in advance for the help.
You can add an optional query parameter after you redirect the first time and combine that with a simple rewrite condition on the query string to check if that parameter exists or no, try this :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/([0-9]+)/([0-9]+)/([0-9]+)/([A-Za-z0-9-]+)$ /blog/post.php?id=$4 [L]
RewriteCond %{QUERY_STRING} !^(.*&)?r=0(&.*)?$
RewriteRule ([^/]+)/?$ /page.php?id=$1&r=0 [L]

htaccess url rewrite with search

i am trying to create an .htaccess file that will achieve the following
create a clean url like this www.mydomain.com/About-Us with page page.php?title=About Us
query a database with the parameters passed on the url like this www.mydomain.com/?search=abc and it pulls to page index.php?search=abc
this is my code so far
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^?search=([0-9]*)$ index.php?id=$1 ## e.g www.mydomain.com/?search=try
RewriteRule ^([A-Z]*)$ page.php?name=$1
## www.mydomain.com/About-Us
##ErrorDocument 404 PageNotavailabale
####Protect the system from machines with worms
RewriteRule (cmd|root)\.exe - [F,E=dontlog:1]
#### To hold and redirect css/images/js files
RewriteRule images/(.+)?$ images/$1 [NC,L]
RewriteRule css/(.+)?$ css/$1 [NC,L]
RewriteRule js/(.+)?$ js/$1 [NC,L]
Why not using this kind of url for your search engine : www.domain.com/search/abc ?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^search/([a-zA-Z0-9]+)$ index.php?search=$1
RewriteRule ^([a-zA-Z0-9]+)$ index.php?page=$1
Then, your access your pages with www.domain.com/<myPage>.
And your search engine with www.domain.com/search/<mySearch>
EDIT :
Please notice your rules doesn't allow a lot of params :
^?search=([0-9]*)$ allows only numbers (even an empty parameter)
^([A-Z]*)$ allows only uppercase letters (and also empty parameter)

Resources