Apache Rewrite rule to add namespace to URL if not present - apache

I am pretty new to Apache.
Consider I have URL like http://www.abc.com/home
I want this URL to be redirected to http://www.abc.com/foo/home.
Basically, I want to add "foo/" to URL if not present. How would I achieve this?

You may try this in one .htaccess file at root directory:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !/foo [NC]
RewriteRule ^home/(.*) /foo/home/$1 [L,NC]
For permanent redirection, replace [L,NC] with [R=301,L,NC]

Related

Mask subdomain using .htaccess

I want to mask my subdomain to a specific url on my main domain using .htaccess.
For example: https://sub.example.com to show the content of https://www.example.com/page/12345.
And the URL is still https://sub.example.com
I have got the following in .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.example\.com
RewriteRule ^https://sub\.example\.com https://www\.example\.com/page/12345
But if I go to https://sub.example.com, it shows content of https://www.example.com. Can't seem to figure out what's wrong. Please help!
Actually, you want to rewrite without redirecting. This requires enabling mod_proxy and mod_rewrite in Apache's httpd.conf.
Then, the rewrite should look like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [NC]
#only when there is query string and https on, if needed
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTPS} =on
RewriteRule ^$ https://www.example.com/page/12345? [R=301,NE,NC,L]
Reference:
What exactly does the Multiviews options in .htaccess?
htaccess rewrite without redirect
Apache: RewriteRule Flags

Apache URL Rewriting - Hide path in URL only when accessing specific path

I would like to rewrite my URL only when typing this:
www.domain.com/dir1/dir2/dir3/
in that path there is a index.html file that will be automatically loaded. And I'd like it to become this:
www.domain.com/index.html
It is important that the root www.domain.com does not redirect to that subdirs because it already redirects to another .php file.
I wrote the following .htaccess file:
RewriteRule ^dir1/dir2/dir3/(.*)$ dir1/dir2/dir3/$1 [L]
but it redirects even when typing the root domain.com.
Could you help me with that?
Edit:
I have tried in another browser and the real problem is that when typing domain.com/dir1/dir2/dir3 or domain.com/dir1/dir2/dir3/index.html the dir1/dir2/dir3/ is still shown and is not hidden.
Edit 2:
my complete .htaccess:
AddHandler application/x-httpd-php56s .php
ErrorDocument 404 /404/404.html
ErrorDocument 500 /404/404.html
RewriteEngine on
RewriteBase /
Options -Indexes
RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /404/404.html [R=404,NC,L]
RewriteCond %{REQUEST_URI} ^/500/$
RewriteRule ^(.*)$ /404/404.html [R=500,NC,L]
Thanks
Replace your current code with this:
DirectorySlash On
RewriteEngine On
# skip all known resources from rewrite rules below
RewriteRule \.(?:jpe?g|gif|bmp|png|ico|tiff|css|js)$ - [L,NC]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+dir1/dir2/dir3/(\S*) [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^(?!dir1/dir2/dir3/)(.*)$ dir1/dir2/dir3/$1 [L,NC]

htaccess get folder one level below

I have some folders in root of my server:
css/
js/
gfx/
new_www/
...
and some more. Assuming that I want in folder new_www copy of current page, but force it to use some things from root folder (for example gfx folder). I've tried following with .htaccess but its not working:
RewriteEngine on
RewriteBase /
RewriteRule /new_www/_js/(.*) /_js/$1 [L]
RewriteRule /new_www/gfx/(.*) /gfx/$1 [L]
What am I doing wrong? .htaccess is placed in root of folder.
Please understand that RewriteRule doesn't match leading slash in an URI when used in .htaccess. Your code should be replaced with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^new_www/(_js/.*)$ /$1 [L,NC]
RewriteRule ^new_www/(gfx/.*)$ /$1 [L,NC]
OR even better will be to have just one rule like this:
RewriteRule ^new_www/(.+)$ /$1 [L,NC]

Why I cannot redirect in htaccess in this case?

I want to redirect the url of a javascript file by using htaccess:
From:
http://www.domain.co.uk/tracking/tracking.js
To:
http://www/domain.co.uk/Templates/domain.co.uk/tracking/tracking.js
After trying many things, now I've this one:
RewriteEngine on
RewriteRule tracking/tracking.js? http://www.domain.co.uk/Templates/domain.co.uk/js/tracking.js [L]
the problem is that with this configuration I only can access to the javascript code (the file) writing ......../tracking/tracking.js/ (with slash in the end, but I dont want it)
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.domain\.co\.uk)$ [NC]
RewriteRule ^(tracking/tracking\.js)$ http://%1/Templates/domain.co.uk/$1 [L,R=301,NC]

Redirect subdomain to another subdomain

i want to know how could i redirect subdomain to another subdomain and also different domain name
for example
i want to redirect subdomain
forum.example.com
to
forum.example.net
thanks
This code should work for you. you need to add a redirect to your .htaccess file.
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.net/$1 [r=301,nc
Another good, but lazy way is to use the following link, to genereate the code for you htaccesss generator
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^forum\.example\.com$ [NC]
RewriteRule ^ http://forum.example.net%{REQUEST_URI} [R,L]