Multiple values for the same variable in mod rewrite - regex

I have the following rules in my htaccess file:
RewriteRule ^animals$ ?filter=$1 [L]
RewriteRule ^plants$ ?filter=$1 [L]
RewriteRule ^tech$ ?filter=$1 [L]
RewriteRule ^social ?filter=$1 [L]
Is there a way to combine these into 1 rule? as the list is actually larger than that and it's the same rule.

Yes it can be combined into one rule using regex OR:
RewriteRule ^(animals|plants|tech|social)/?$ ?filter=$1 [L,NC,QSA]

Related

Rewrite rule for specific name/value to take precedence over others

I have two rewrite rules:
RewriteRule ^([^/]+)/([^/]+)/?$ /?action=viewCategoryName&categoryName=$1&page_identifier=$2 [L,QSA,B]
and
RewriteRule ^([^/]+)/?$ /?action=viewHomepageName&page_identifier=$1 [L,QSA,B]
I would like to create a new rule to rewrite:
/?action=archive&categoryId=$1
to
/archive/news
Where 'news' is the category Id, or $1.
With the two rules mentioned above, is this possible? I would imagine I need to place it above the other rules, and specify specifically if example.com/archive/ rewrite and then stop the rewrite.
You can have your rules like this in root .htaccess
RewriteEngine On
# don't process further rules for real files and directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^(archive)/([^/]+)/?$ /?action=$1&categoryId=$2 [L,QSA,NC]
RewriteRule ^([^/]+)/([^/]+)/?$ /?action=viewCategoryName&categoryName=$1&page_identifier=$2 [L,QSA,B]
RewriteRule ^([^/]+)/?$ /?action=viewHomepageName&page_identifier=$1 [L,QSA,B]

htaccess disallow root directory but allow other urls

I'm using Xfilesharing for our company and I need to block the access to the main upload page by our internal IP range 192.168.230.1-225 and allow the download to every IP.
Using the following rule will block the main page but also the rest. But using a rule to allow the download breaks the rewrite rules.
RewriteEngine on
RewriteRule ^$ /cgi-bin/index.cgi [L]
RewriteCond %{REMOTE_ADDR} ^192\.168\.230\.25[0-5]|2[0-4]\d|1?\d\d?$
RewriteRule ^([0-9A-Z]{12})/([^/]+)/?$ /cgi-bin/index.cgi?act=download1&id=$1&fname=$2 [L,QSA]
RewriteRule ^.*$ /error [R=302,L]
RewriteRule ^del\-([0-9A-Z]{12})\-([0-9A-Z]+)/.+$ /cgi-bin/index.cgi?del=$1-$2 [L]
RewriteRule ^admin$ /?act=admin [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z0-9\-\_]+).html$ /cgi-bin/index.cgi?act=page&tmpl=$1 [L]
The literal URL is http://www.example.com/MG4YBAOW18QH/3.png and the rule: ^([0-9A-Z]{12})/([^\/]+)$ /cgi-bin/index.cgi?act=download1&id=$1&fname=$2 [L]
Have your download rules like this:
RewriteCond %{REMOTE_ADDR} ^192\.168\.230\.\d+
RewriteRule ^([0-9A-Z]{12})/([^/]+)/?$ - [F,NC]
RewriteRule ^([0-9A-Z]{12})/([^/]+)/?$ /cgi-bin/index.cgi?act=download1&id=$1&fname=$2 [L,QSA,NC]

htaccess rewrite rule: setting first parameter as optional

I'm trying to set the first rule as optional, so both of these URLs will work:
/username
/username/history
/history
The username will never clash.
RewriteRule ^([a-z]+)/((history|analysis|messages|photos)?)$ pages/dashboard.php?username=$1&page=$2 [L]
With the above I have /username/history working. Cannot figure howto get the others.
EDIT: The above snippet is the result of trying to merge these three lines into one.
RewriteRule ^(history|analysis|messages|photos)?$ pages/dashboard.php?page=$1 [L]
RewriteRule ^([a-z]+)/(history|analysis|messages|photos)?$ pages/dashboard.php?username=$1&page=$2 [L]
RewriteRule ^([a-z]+)$ pages/dashboard.php?username=$1 [L]
You can use this rule:
RewriteRule ^(history|analysis|messages|photos)/?$ pages/dashboard.php?page=$1 [L,NC,QSA]
RewriteRule ^([a-z]+)(?:/(history|analysis|messages|photos))?/?$ pages/dashboard.php?username=$1&page=$2 [L,NC,QSA]

.htaccess RewriteRule Conflict Error

I have htaccess file problem. I think my htaccess files conflict.
my .htaccess files:
RewriteEngine on
RewriteRule ^(.*)/video/ video.php?vid=$1 [L]
RewriteRule ^search/(.*)\.html search.php?q=$1 [L]
RewriteRule sitemap-(.*).xml sitemap.php?sayfa=$1 [L]
and my url
Search : http://www.domain.com/search/search_query.html
Video Play : http://www.domain.com/path1/video/video_name.html
.htaccess file conflict video.php and search.php
pls help me
Change order of your rules to keep specific ones first and generic ones later:
RewriteEngine on
RewriteRule ^search/(.*)\.html$ search.php?q=$1 [L]
RewriteRule sitemap-(.*).xml$ sitemap.php?sayfa=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/video/ video.php?vid=$1 [L,NC]
Try using a negative lookahead to prevent the two rules from getting applied to each other:
RewriteEngine on
RewriteRule ^(?!search)(.*)/video/ video.php?vid=$1 [L]
RewriteRule ^search/(?!video)(.*)\.html search.php?q=$1 [L]

Rewrite URL with 1 or more parameters?

I'm strugglening with my .htaccess file in orther to achieve this:
a.com/male-items (OR)
a.com/male-items/popularity -> a.com/index.php?g=m&sort-popularity
a.com/female-items (OR)
a.com/female-items/popularity -> a.com/index.php?g=f&sort=popularity
a.com/male-items/alphabet -> a.com/index.php?g=m&sort=alphabet
a.com/male-items/alphabet/a -> a.com/index.php?g=m&sort=alphabet&l=a
(and same for female)
I know it should be something like
RewriteRule ^a$ a.com/index.php?q=$1
But actually looking into the different mod-rewrite / regex explanations and cheat-sheets doesn't help a lot with getting it to work. The hard part is to understand how do you define the different parametes in the address and then use them in the rewritten url.
(any explanations with your solution would be appretiated)
Use these rules in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^male-items/?$ /index.php?g=m&sort=popularity [L,QSA]
RewriteRule ^male-items/([^/]+)/?$ /index.php?g=m&sort=$2 [L,QSA]
RewriteRule ^male-items/([^/]+)/([^/]+)/?$ /index.php?g=m&sort=$2&l=$3 [L,QSA]
RewriteRule ^female-items/?$ /index.php?g=f&sort=popularity [L,QSA]
RewriteRule ^female-items/([^/]+)/?$ /index.php?g=f&sort=$2 [L,QSA]
RewriteRule ^female-items/([^/]+)/([^/]+)/?$ /index.php?g=f&sort=$2&l=$3 [L,QSA]
These htaccess lines redirect all nonexisting files/folders to index.php:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
inside index.php you can use: $_SERVER['REQUEST_URI'] to parse your parameters.