HTACCESS multiple subfolders- best practice? - .htaccess

Here is the code I use to get three virtual subfolders with .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?url=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?url=$1&sub=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?url=$1&sub=$2&subsub=$3 [L]
the question is: is it possible to make it simpler? For example:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?url=$1&sub=$2&subsub=$3 [L]
I know this won't work. I am just trying to figure out if it is the only way to add more subfolders. What if I need, let's say, six (url/sub/sub2/sub3/sub4/sub5....etc)??
Thank you in advance

Related

Use 2 urls for the same directory using htaccess

Is it possible to using htaccess?
I want to keep my original url "menu", but also use /en/menu/ for it.
Here is my code so far (sorry I am terrible at htaccess):
RewriteEngine On
RewriteBase /server/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/menu/ [NC]
RewriteRule ^([^/]+)/([^/]+)en/menu/ [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /menu/ [L,QSA]

How to separate two variables with a hyphen using htaccess

I'm trying to do some url like that: domain.com/category-subcategory.
If I go to domain.com/category Its work, but if I trying to go to domain.com/category-subcategory it is not working..
this is my code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ category.php?category=$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)-([^/]+)/$ category.php?category=$1&subcategory=$2 [L,NC]
How can I fix it? Thanks
Edit: I fixed it! here is the code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)-([^/]+)$ category.php?category=$1&subcategory=$2 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ category.php?category=$1 [L,NC]
I was only need to remove the slash before the $ at the end. Thanks for everyone!
Try with below,
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)-([^/]+)$ category.php?category=$1&subcategory=$2 [L,NC]

rewriterule dynamic with sub

I can't fix this issue. I'm trying to get rewrite rules to work with dynamic urls like:
http://example.com/[CATEGORY]/[PAGE]/[OPTIONS]
or
http://example.com/[PAGE]
This is my .htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} ^(.*[^/])
RewriteRule ^(.*[^/]) /index.php?url=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} ^(.*[^/])\/^(.*[^/])
RewriteRule ^(.*[^/])\/^(.*[^/]) /index.php?url=$1/$2 [QSA,L]
The first rewriterule is working, but the second isn't. Also is this not clean.
Anyone who knows this?

htaccess rewrite index.php on root and subfolders

I'm trying to redirect all requests to ./index.php?site=$1 while I only want to use the part behind the last slash.
So I want www.mydomain.com/firstpage to become www.mydomain.com/index.php?site=firstpage and www.mydomain.com/subfolder/anotherpage to become www.mydomain.com/subfolder/index.php?site=anotherpage
But right now www.mydomain.com/subfolder/anotherpage becomes
www.mydomain.com/index.php?site=subfolder/anotherpage
This is what I've got:
RewriteEngine on
Options +SymlinksIfOwnerMatch
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?site=$1 [L]
What could I do to redirect only the part after the last slash?
Thank you so much!
SOLUTION:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*/)?([^/]+)$ $1index.php?site=$2 [L]
Found this other, on my opinion more elegant solution. By this, the number of subfolders is irrelevant.
SOLUTION:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*/)?([^/]+)$ $1index.php?site=$2 [L]
Finally figured out the answer myself.
At first I tried putting more RewriteRules under the same RewriteCond-lines gave me server errors. So I duplicated the RewriteCond-lines for the subdirectories:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ $1/index.php?site=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?site=$1 [L]

mod_rewrite, matching a specific pattern

The title is quite ambiguous so I apologise.
The problem lies in the following code. I need to add actions after the keyword statement however I do not want the action to override the keyword when action is not present.
The input url with actions is http://websitenamehere/uk/en/meet-us/update
and without: http://websitenamehere/uk/en/meet-us
So I want the actions clause to be triggered only when what proceeds it is a string of 4 or more characters. This htaccess rule should not depend on the country or lang being set when making the comparison.
If I have missed anything please let me know and I will update the question. I know it should be fairly simple but at the moment I am just blundering through htaccess and leanring as I go.
#language
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]{2})/([a-z]{2})(?:/(.*)|)$ /$1/$3?lang=$2 [NC,QSA,PT]
#country
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]{2})(?:/(.*)|)$ /$2?country=$1 [NC,QSA,PT]
#keyword
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/(.*)/$ /$2?keyword=$1 [NC,QSA,PT]
#actions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteRule ^([^/]+)/(.*)$ /?action=$1 [NC,QSA,PT]
#withoutactions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteRule ^([^/]+)/(.*)$ /$2?keyword=$1 [NC,QSA,PT]
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/$ /$1.php [NC,QSA,PT]
I figured this out today. Basically what is happening is the htaccess file when building a back reference goes through the rules more than once.
I did add a skip into the original keyword line but it would simply be ignored:
#keyword
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/(.*)/$ /$2?keyword=$1 [NC,QSA,PT,S=1]
#withoutactions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteRule ^([^/]+)/(.*)$ /$2?keyword=$1 [NC,QSA,PT]
#actions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteRule ^([^/]+)/(.*)$ /?action=$1 [NC,QSA,PT]
When the htaccess ran the second time the S would no longer apply and it would then hit the withoutactions block. To circumvent this I simply checked if keyword already existed in the query string and if so to skip the rule:
#keyword
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/(.*)/$ /$2?keyword=$1 [NC,QSA,PT,S=1]
#withoutactions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteCond %{QUERY_STRING} !keyword
RewriteRule ^([^/]+)/(.*)$ /$2?keyword=$1 [NC,QSA,PT]
#actions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteRule ^([^/]+)/(.*)$ /?action=$1 [NC,QSA,PT]
I hope this helps someone experiencing a similar problem with back references!

Resources