htaccess RewriteCond RewriteRule - apache

I already have website www.example.com which resides in /public_html/site.
Now I want to point www.example.com/anothersite to /public_html/anothersite.
How can I?
The .htaccess in /public_html:
RewriteOptions inherit
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} !site/
RewriteRule (.*) /site/$1 [L]
The .htaccess in /public_html/site:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /site/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /site/index.php [L]
</IfModule>
# END WordPress
In The .htaccess in /public_html I have tried this but it says error 404 Page not found!:
RewriteOptions inherit
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^anothersite/(.*)$ http://example.com/anothersite/$1 [R=301,NC,L]
RewriteCond %{REQUEST_URI} !site/
RewriteRule (.*) /site/$1 [L]
But I dont know why it was not working last but now its working with the above code...

The .htaccess in /public_html:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ [NC]
RewriteRule (?!^(?:site|anothersite)(?:/.*|)$)^.*$ /site%{REQUEST_URI} [L]

Related

htaccess: assets redirection returns 404

I want to redirect the assets requests to the correct real path, my website root folder contains the following files:
.htaccess
main-site/
new-site/
the root .htaccess is as follow:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^new\.mywebsite\.com$
RewriteRule ^(.*)$ new-site/index.php [L]
RewriteCond %{HTTP_HOST} ^www\.mywebsite\.com$
RewriteRule ^(.*)$ main-site/frontend/web/index.php [L]
# Non-www to www redirect
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
then inside main-site dir, I have the following htaccess:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/main-site/(assets|css|images|js)/
RewriteRule ^/main-site/assets/(.*)$ frontend/web/assets/$1 [L]
RewriteRule ^/main-site/css/(.*)$ frontend/web/css/$1 [L]
RewriteRule ^/main-site/js/(.*)$ frontend/web/js/$1 [L]
RewriteRule ^/main-site/images/(.*)$ main-site/frontend/web/images/$1 [L]
RewriteCond %{REQUEST_URI} !^/(frontend|backend)/web/(assets|css|images|js)/
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ frontend/web/index.php
</IfModule>
but any request to the assets like this will always return 404:
http://www.mywebsite.com/main-site/plugins/bootstrap-3.3.7/js/bootstrap.js

.htaccess http www or non www redirect to https

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteEngine On
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
This is rules in .htaccess, urls like http://example.com/test, http://www.example.com/test doesn't redirect to https, www redirect to non-https address. http://example.com url redirected properly. What's wrong?
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Rule moved to top and work's properly.

Using directores for each domain using htaccess

I have a host and some domains. I want use a directory for each domain. Unfortunately my host dose't support vhosts. I made .htaccess file in root to redirect every domain request to it own directory.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} domain1\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /domain1/$1 [L]
RewriteCond %{HTTP_HOST} domain2\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /domain2t/$1 [L]
</IfModule>
It worked well and my site accessabe with "http://domain1.com" address. I try to use CodeIgniter as my sites framework.
So I configure base-url in CI config file to "http://domain1.com/domain1/" but it worked for main page only and when I request another controller like "http://doamin1.com/somecontroller", the page not found.
Also I setup .htaccess in site directory as follow.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /domain1
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteRule ^$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(index\.php|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
How can I do this job so that browser addressbar do't show directory like
"http://doamain1.com/domain1/somecotroller" (this form is working).
Tanks everybody.
UPDATE
My routes.php.
$route['default_controller'] = 'main';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
I after some tests got the answer that I need.
I change root .htaccess like this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/domain2/
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.
RewriteRule ^(.*)$ /domain2/$1 [L]
RewriteCond %{REQUEST_URI} !^/domain1/
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.
RewriteRule ^(.*)$ /domain1/$1 [L]
</IfModule>
And every .htaccess in domain directory to as follow
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /domain1
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
</IfModule>
Also change config.php file base-url to "http://domain1.com/"

Redirecting HTTP to HTTPS correctly in Question2Answer script

I have searched for this question for many days and tried so many different methods but nothing works so far. I am using the Question2Answer script and I want to redirect all the HTTP requests to HTTPS.
My URL structure is set to :
/123/why-do-birds-sing (requires htaccess file)
and the htaccess file is as follow:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
</IfModule>
This correctly redirects http://example.com to https://example.com. However, if the user enters an address like this : www.example.com/users they are redirected to https://example.com/index.php?qa-rewrite=users which returns a 404 error.
The index.php?qa-rewrite= is added automatically and removing it from the htaccess totally messes up everything and I think it should be there.
This is because you need all of your redirect rules before any of the routing rules (the ones without the R flag), so:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
</IfModule>
I use this:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^(.*)$ "https\:\/\/example\.com\/$1" [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qa-rewrite=$0&%{QUERY_STRING} [L]
</IfModule>
If you don't like to redirect www, you can remove the line
RewriteCond %{HTTP_HOST} ^www\.example\.com$ and the [OR] above

Everything redirecting to subdomain redirect htaccess

So I have the following code:
ServerName test.com
ServerAlias *.test.com
DocumentRoot /var/www/test.com/public_html
Options All -Indexes
FileETag none
ServerSignature Off
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} .+
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^(.*)\.test\.com$ [NC]
RewriteRule ^$ /%1.php [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^([^.]+)\.test\.com$ [NC]
RewriteRule ^(.+)/?$ /%1.php?o=$1 [L,NC,QSA]
</IfModule>
On my old server I had this in a .htaccess file (minus the documentroot part) and it worked fine doing this:
sub.test.com -> test.com/sub.php
It still does this correctly, but now that I have moved it into the sites-available config file, if I try to access a file, it breaks and uses that same rule as the file to get:
sub.test.com/file.css -> test.com/sub.php
Is there a way to keep it from doing this? (I tried doing RewriteCond %{REQUEST_FILENAME} !-f and such but it didn't seem to care much about that rule).
Thanks
Inside Apache config try this code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} .+
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^(.*)\.test\.com$ [NC]
RewriteRule ^/?$ /%1.php [L,NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^([^.]+)\.test\.com$ [NC]
RewriteRule ^/?(.+?)/?$ /%1.php?o=$1 [L,NC,QSA]
</IfModule>
Apache config matches a leading slash in RewriteRule unlike htaccess