htaccess ReWrite for images - .htaccess

I have this code in my .htaccess file which redirects all web pages inside the main_website folder but currently images are stored in the root
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(blog)/(post|tags|category)/([\w-]+)/?$ main_website/index.php?id=$1&type=$2&unique=$3 [QSA,NC,L]
RewriteRule ^(blog)/(archives)/([0-9]{4})/([0-9]{2})?$ main_website/index.php?id=$1&type=$2&year=$3&month=$4 [QSA,NC,L]
RewriteRule ^(support/knowledgebase)/(article|category|search)/([\w-]+)/?([0-9]+)?$ main_website/index.php?id=$1&type=$2&unique=$3&pagenum=$4 [QSA,NC,L]
RewriteRule ^(account/hosting)/([\w-]+)?$ main_website/index.php?id=$1&p=$2 [QSA,NC,L]
# This will process the /section/(.*) requests, ?section=1 will be appended to query string
RewriteRule ^section\/(.*)?$ main_website/index.php?section=1&id=$1 [L,QSA]
RewriteRule ^/?$ main_website/index.php [L,QSA]
RewriteRule ^([a-zA-Z0-9-/_]+)/?$ main_website/index.php?id=$1 [L,QSA]
How can i alter the above to move my images folder to the main_website folder?

Move your images folder under main_website/ and then you can use these rules in your site root .htaccess:
RewriteEngine On
RewriteRule ^/?$ main_website/index.php [L]
# redirect images to main_website/images/
RewriteRule ^images/.+$ /main_website/$0 [L,NC,R=301,NE]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(blog)/(post|tags|category)/([\w-]+)/?$ main_website/index.php?id=$1&type=$2&unique=$3 [QSA,NC,L]
RewriteRule ^(blog)/(archives)/([0-9]{4})/([0-9]{2})?$ main_website/index.php?id=$1&type=$2&year=$3&month=$4 [QSA,NC,L]
RewriteRule ^(support/knowledgebase)/(article|category|search)/([\w-]+)/?([0-9]+)?$ main_website/index.php?id=$1&type=$2&unique=$3&pagenum=$4 [QSA,NC,L]
RewriteRule ^(account/hosting)/([\w-]+)?$ main_website/index.php?id=$1&p=$2 [QSA,NC,L]
# This will process the /section/(.*) requests, ?section=1 will be appended to query string
RewriteRule ^section\/(.*)?$ main_website/index.php?section=1&id=$1 [L,QSA]
RewriteRule ^([\w/-]+)/?$ main_website/index.php?id=$1 [L,QSA]

Related

htaccess rule issue in redirecting the url

I want to redirect all below pattern url to before "?" url
i.e
Xyz.com/worksheet/rebus/?random=testing11
Xyz.com/worksheet/rebus/?abc
Xyz.com/worksheet/rebus/?l=1
should be redirected to
Xyz.com/worksheet/rebus/
I tried many things but not able to succeed. How can i do using .htaccess
Rules tried
RewriteBase /worksheet/
RewriteRule ^rebus/?$ /worksheet/rebus/ [L,R=301]
Overall
RewriteEngine On
RewriteBase /worksheet/
RewriteCond %{THE_REQUEST} \s/(rebus)/\?(\S+)\s [NC]
RewriteRule ^ /worksheet/%1/ [R=301,L]
#RewriteRule ^rebus/?$ /worksheet/rebus [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)?$ index.php?url=$2&tableName=$1&showSol=$3 [L,QSA]
Have it this way:
RewriteEngine On
RewriteBase /worksheet/
# \?\S matches at least one character after ?
RewriteCond %{THE_REQUEST} \s/(worksheet/rebus)/\?\S [NC]
RewriteRule ^ /%1/? [R=301,L]
RewriteRule ^rebus/?$ /worksheet/rebus? [L,R=301,NC]
# skip all rules for real files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?url=$2&tableName=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?url=$2&tableName=$1&showSol=$3 [L,QSA]

Multiple websites pointing to the same directory rewrite url to point different folders

I have 3+ websites, example:
www.example.com/requestedpage
www.esempio.it/requestedpage
primer.ru/requestedpage
All websites points to the same server's directory. There are 3 directories in it: "en", "it", "ru" with localized requestedpage in them. I want to internally redirect those requests (only PHP, not images) to folders without changing URL. So .com website read pages from "en" folder it - "it", ru - "ru" folder but user shouldn't see that.
I tried some things but they doesn't work..
RewriteCond ^(.+)\.(ru)$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/ru/$1 [NC,L]
RewriteCond %{HTTP_HOST} ^(.+)\.(ru)$
RewriteRule ^(.+)\.php$ ru%{REQUEST_URI} [NC,L]
RewriteCond %{HTTP_HOST} MYWEBSITE.ru$ [NC]
RewriteCond %{HTTP_HOST} !ru/
RewriteRule ^(.*)$ /ru/$1 [NC,L]
After a lot of tests and thanks to anubhava help I found a way to deal with it, not nice way, and I think it should be improved:
# ===== MULTILANGUAGE WEBSITES =====
#Rule for IT website
RewriteCond %{HTTP_HOST} !ITALIANDOMAIN\.it$
RewriteRule .* - [S=3]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ it/$1/$2
RewriteRule ^([^/]+)$ it/$1
RewriteRule ^$ it/ [NC,L]
#Rule for FR website
RewriteCond %{HTTP_HOST} !FRANCEDOMAIN\.com$
RewriteRule .* - [S=3]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ fr/$1/$2
RewriteRule ^([^/]+)$ fr/$1
RewriteRule ^$ fr/ [NC,L]
#Rule for RU website
RewriteCond %{HTTP_HOST} !(RUSSIANDOMAIN1\.com)$
RewriteCond %{HTTP_HOST} !(xn--10-olc1a0ag\.xn--p1ai)$
RewriteRule .* - [S=3]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ ru/$1/$2
RewriteRule ^([^/]+)$ ru/$1
RewriteRule ^$ ru/ [NC,L]
You can use this rule from a .htaccess placed inside the parent folder of of it, en, ru folders:
RewriteEngine On
# skip all files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} \.(ru|it)$
RewriteCond %{REQUEST_URI} !^/(?:ru|it)/ [NC]
RewriteRule ^.*$ %1/$0 [L]
RewriteCond %{HTTP_HOST} \.com$
RewriteCond %{REQUEST_URI} !^/en/ [NC]
RewriteRule ^.*$ en/$0 [L]

Removing index.php and another page inside the root folder from URL with .htaccess

In my Root folder I have index.php and profile.php.
Currently I'm using these rules for rewrite my index.php
from example.com/index.php?a=profile&u=user1 to example.com/profile&u=user1
RewriteEngine On
RewriteCond %{request_filename} -f
RewriteRule ^(.*) $1 [L]
RewriteRule ^(([^/]*)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L]
RewriteRule ^welcome/([^/]+)/?$ index.php?a=welcome [NC]
RewriteRule ^page/([^/]+)/?$ index.php?a=page&filter=$1 [NC]
It works fine but now I want to rewrite my URL for profile.php page too, like this:
from example.com/profile.php?u=user1 to example.com/user1
How can I edit my .htaccess to rewrite profile.php together?
Based on question and comments above here are the rules that should work for you:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^profile/([^/]+)/?$ profile.php?u=$1 [NC,QSA,L]
RewriteRule ^welcome/([^/]+)/?$ index.php?a=welcome&filter=$1 [NC,QSA,L]
RewriteRule ^page/([^/]+)/?$ index.php?a=page&filter=$1 [NC,QSA,L]
RewriteRule ^(([^/]+)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L,QSA]

htaccess issue when i try to access a directory

So here's my code:
RewriteEngine On
RewriteRule ^contact$ contact.php [QSA,L]
RewriteRule ^error$ error.php [QSA,L]
RewriteRule ^([^/.]+)/?$ product.php?weblink=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /error [L,R=301]
It's simple so when I try to access host/product-name it works but when I try to access for example a directory /blog or /blog/ redirect pe to error page specified by last rule. What's the problem?
You can do it this way
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
RewriteRule ^contact$ contact.php [QSA,L]
RewriteRule ^error$ error.php [QSA,L]
RewriteRule ^([^/.]+)/?$ product.php?weblink=$1 [L,QSA]
RewriteRule . /error [L,R=301]

don't rewrite static css/js/img files

I'm trying to get my htaccess file not to rewrite my static files (js/css/images).
This is my current htaccess file:
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule !\.(jpg|css|js|gif|png)$ public/ [L]
RewriteRule !\.(jpg|css|js|gif|png)$ public/index.php?url=$1
How do I rewrite it?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^.*\.(jpg|css|js|gif|png)$ [NC]
RewriteRule ^(.*)$ public/index.php?url=$1
All requests to not existing files which doesn't end with listed extensions (case nonsensitive match) are rewritten to public/index.php passing the current URL as url= GET argument
This only works for the one line below it. So if you have many RewriteRules it could help to use something like this:
RewriteEngine on
RewriteRule ^(.*?)\.(php|css|js|jpg|jpeg|png|pdf)$ - [L]
RewriteRule ^(.+)/(.+)/?$ index.php?page=$1&subpage=$2 [L]
RewriteRule ^(.+)$ index.php?page=$1 [L]
for posterity.
Maybe my rules will be useful
\www\.htaccess
#file: \www\.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} \.(css|js|html?|png|gif|jpg|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?)$ [NC]
RewriteRule (.*\.[css|js|html?|png|gif|jpg|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?]+) public/$1?a [QSA,L]
RewriteCond %{REQUEST_URI} \.(css|js|html?|png|gif|jpg|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?) [NC]
RewriteRule (.*\.[css|js|html?|png|gif|jpg|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?]+) public/$1 [QSA,L]
#RUN TEST
RewriteCond %{REQUEST_URI} !\.(css|js|html?|png|gif|jpe?g|svg|xls.?|pdf|docx?|eot|ttf|woff2?)$ [NC]
RewriteRule ^(.*)$ index.php?REQUEST_FILENAME=%{REQUEST_FILENAME}&date=$1&REQUEST_URI=%{REQUEST_URI} [QSA,L]
#END TEST
RewriteRule . index.php?url=%{REQUEST_URI} [QSA,L]
ErrorDocument 404 index.php?error=404
\www\index.php
<?php
// file: /www/index.php
var_dump($_GET);
\www\app\.htaccess
#file: \www\app\.htaccess`
RewriteEngine On
RewriteRule ^(.*)$ ../_%{REQUEST_URI} [QSA,L]
\www\public\.htaccess
#file: \www\public\.htacces`
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=%{REQUEST_URI}&rewrite=$1 [QSA,L]
/www/public/index.php
<?php
// file: /www/public/index.php
var_dump($_GET, __DIR__);

Resources