htaccess rewrite folder with a question mark - apache

I want to rewrite my folder with a question mark. Is this possible?
It would look like this:
My link now:
http://domain.com/folder/page
Wanted result:
http://domain.com/?page
or (in the worst case):
http://domain.com/page
BTW, I am using this code in my htaccess to access .php files without .php:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Yes it is possible with this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^folder/ folder%{REQUEST_UEI} [L,NC]

Related

Handle languages in .htaccess

I want to solve the next problem:
1) When entering the url without language, use English language as default
example:
http//localhost/plants/ or http//localhost/plants
or
http//localhost/plants/shop/accessories
2) When entering the url with language, pass that parameter as the language to use
example:
http//localhost/plants/es/shop/accessories
or
http//localhost/plants/es/ or http//localhost/plants/es
So far I have tried:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(en|es)/(.*)$ index.php?url=$2&lang=$1 [L,QSA]
RewriteRule ^(.*)$ index.php?url=$1&lang=en [L,QSA]
If I comment the first RewriteRule and enter any url it works fine but always using the en language.
If I comment the second RewriteRule and I use a url with the language:
http//localhost/plants/es/
or
http//localhost/plants/es/shop/accessories
It works fine, but it doesn't set the default language to English when is not given.
Any idea why it doesn't work when I leave the two rules?
Thanks
Ps: I have removed the : after http
If plants is your document root, you should be able to use this rule.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} !/(en|es)
RewriteRule ^(.*)/?$ index.php?url=$1&lang=en [L,QSA]
RewriteRule ^(en|es)/?(.*)/?$ index.php?url=$2&lang=$1 [L,QSA]
Let me know how it works for you.
Found a solution doing this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(en|es)/(.*)$ index.php?url=$2&lang=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1&lang=en [L,QSA]
How can I improve this? It feels wrong I have to write twto time the same Cond
Put the following htaccess file inside the /plants directory:
RewriteEngine On
RewriteBase /plants
RewriteCond %{REQUEST_URI} !^/plants/e[ns](/|$) [NC]
RewriteCond %{QUERY_STRING} !lang=e[ns] [NC]
RewriteRule ^.*$ /plants/en/$0 [R=301,L]
RewriteRule ^(e[ns])/(.*)$ index.php?url=$2&lang=$1 [NC,L,QSA]
If the above still doesn't work, update your own approach to something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^(en|es)/(.*)$ index.php?url=$2&lang=$1 [L,QSA]
RewriteRule ^(.*)$ index.php?url=$1&lang=en [L,QSA]

I want to be able to search in two sub-directories with .htaccess

I want to be able to search in two sub-directories with .htaccess
I have the following directory structure
My problem is:
When for example a request on
resources/images/players_small_pigs/BMI10.png
is being received, I would like to be able to search in the path
webapp/public/resources/images/players_small_pigs/BMI10.png
is the file not to find here, I would like to continue the search in
webapp/adminpanel/resources/images/players_small_pigs/BMI10.png
Can some one help me to get it to work by using the .htaccess file? The problem throughout the entire folder structure resources, views, and webservices, I think it might be a bad structure I have and maybe I should put them all in a folder, rather than having it split up between a adminpanel and public folder, what would you recommend?
I have the following code in .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(resources/.*)$ /webapp/public/$1 [L]
RewriteRule ^(resources/.*)$ /webapp/adminpanel/$1 [L]
RewriteRule ^webservices/(.*)$ /webapp/public/webservices/$1 [L]
RewriteRule ^webservices/(.*)$ /webapp/adminpanel/webservices/$1 [L]
RewriteRule ^(public/resources/images/)(sponsors/)?(.*)$ /webapp/$1$2$3 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} adminpanel
RewriteRule (.*) /webapp/adminpanel/views/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !adminpanel
RewriteRule (.*) /webapp/public/views/$1 [L]
RewriteRule ^$ /webapp/public/views/index.php [L]
You can give the following rule a try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/resources
RewriteCond %{DOCUMENT_ROOT}/webapp/public%{REQUEST_URI} -f
RewriteRule ^ %{DOCUMENT_ROOT}/webapp/public%{REQUEST_URI} [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/resources
RewriteCond %{DOCUMENT_ROOT}/webapp/adminpanel%{REQUEST_URI} -f
RewriteRule ^ %{DOCUMENT_ROOT}/webapp/adminpanel%{REQUEST_URI} [L]

How to stop re-write rules for existing files located in virtual language folders (.htaccess)

In my .htaccess all requested urls that can't be found are being rewritten to main.php using the code below:
RewriteCond $1 ^
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ main.php?document=$1 [L,QSA]
This means if someone is typing www.my-site.com/contact_us.php that page will show since that file exists and the rewrite rule above will not be applied.
The problem is when using multi-languages, since I use a virtual folder for each language. For example Italian will be:
www.my-site.com/it/contact_us.php
How can I stop the re-write rule above for all existing files located in these virtual folders?
You can use your rule like this:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{DOCUMENT_ROOT}/$1/$2 !-f
RewriteCond %{DOCUMENT_ROOT}/$1/$2 !-d
RewriteRule ^([a-z]{2})/(.*)$ main.php?lang=$1&document=$2 [L,QSA,NC]
RewriteRule ^(.+)$ main.php?document=$1 [L,QSA]
RewriteCond $1 ^
RewriteRule ^([a-z]{2})/(.*)$ main.php?lang=$1&document=$2 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ main.php?document=$1 [L,QSA]

Apache .htaccess to hide both .php and .html extentions

What lines should I add to remove both .html and .php extensions from my site?
I use this for just the .html part:
RewriteEngine on
RewriteBase /
RewriteCond %{http://jobler.ro/} !(\.[^./]+)$
RewriteCond %{REQUEST_fileNAME} !-d
RewriteCond %{REQUEST_fileNAME} !-f
RewriteRule (.*) /$1.html [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.html\ HTTP
RewriteRule ^([^.]+)\.html$ http://jobler.ro/$1 [R=301,L]
but I need for both file type extensions to be hidden from the same domain.
I know its late to answer but I giving it since it can be useful to someone :)
#Remove php extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
#Remove html extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
This will remove both php and html extension from your files and works perfectly.
You would want to test whether the file with .html or .php exists, and then redirect to such file if so.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_URI} \.((html)|(php))$
RewriteRule ^(.*)\.([^.]+)$ $1 [R,L]

Add directory in the middle of a URL with hataccess

My current .htacces file looks like this:
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
What I am trying to accomplish (as well as the above) is to change a url such as http://domain.com/pages/pagename to something like http://domain.com/index.php/pages/view/pagename.
Keeping in mind that I still require urls without the /page/ part such as http://domain.com/search to go to http://domain.com/index.php/search. I am working with CodeIgniter.
What I have come up with so far is:
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
RewriteCond %{REQUEST_URI} ^/pages/(.*)
RewriteRule ^(.*)$ ./index.php/pages/index/$1 [L,QSA]
But it isn't working.
Try this one:
RewriteEngine on
# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# /pages/ specific rule
RewriteRule ^pages/(.*)$ index.php/pages/index/$1 [L]
# everything else
RewriteRule ^(.*)$ index.php/$1 [L]
The above assumes that http://domain.com/pages/ is not a real folder.
RewriteRule ^pages/(.*) /index.php/pages/index/$1