url rewriting to virtual path keeping always the virtual path - apache

I have a physical folder:
http://www.myowndomain.com/path1/
and I want that all requests to http://www.myowndomain.com/path1/* and http://www.myowndomain.com/path2/* (that is a virtual folder, it doesn't exists) will be redirected to http://www.myowndomain.com/path1/*. Moreover I want all those requests show in the url bar
http://www.myowndomain.com/path2/*
Where should I put .htaccess file and what would it be its content?

Add the following rule to you /root.htaccess
RewriteEngine on
#1)externally Redirect /path1/* => /path2/*##
RewriteCond %{THE_REQUEST} /path1/(.*)\sHTTP [NC]
RewriteRule ^ /path2/%1 [NE,L,R]
#2)internally Redirect /path2/* => /path1/*##
RewriteRule ^/?path2/(.*)$ /path1/$1 [NC,L]
This will internally redirect /path2/ to /path1 .

Related

Subdomain and it's folder rewrite

Let's explain my issues. I have folder which contains subdomain files.
Folder which contains subdomain files is called "subdomain".
My domain is example.com.
My subdomain is sub.example.com.
Issue is that when I type https://sub.example.com it works but same content is loaded when I type https://example.com/subdomain or https://sub.example.com/subdomain
My question is, how to achieve that https://example.com/subdomain(.*) will redirect to https://sub.example.com(.*) and same with https://sub.example.com/subdomain(.*) that will redirect to https://sub.example.com(.*)
Current .htaccess configuration is as follows:
RewriteCond %{HTTP_HOST} ^sub\.example\.com$
RewriteRule (.*) /subdomain/%1%{REQUEST_URI} [L]
You can use this redirect rule as your topmost rule inside your site root .htaccess or in subdomain/.htaccess:
RewriteEngine On
# use THE_REQUEST to detect presence of /subdomain/ in original URL
RewriteCond %{THE_REQUEST} \s/+subdomain/(\S*) [NC]
RewriteRule ^ https://sub.example.com/%1 [L,NE,R=301]
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of other rewrite directives. Example value of this variable is GET /index.php?id=123 HTTP/1.1

Apache mod_rewrite from a folder to a different domain doesn't work with trailing slash

I'm trying to rewrite URLs from old.domain.tld/project to domain.tld/subfolder/project using .htaccess in the project directory on the old server. I think I need to check for the host name so the rule only applies to the old server, even if the .htaccess file is also put on the new server.
I've tried the following .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old\.domain\.tld$ [NC]
RewriteRule ^(.*)$ https://domain.tld/subfolder/project/$1 [R=301,L]
This works fine for URLs like https://old.domain.tld/project/index.php?p=q (redirects to https://domain.tld/subfolder/project/index.php?p=q) but not for the URL without the trailing slash—https://old.domain.tld/project ends up being redirected to https://domain.tld. Very odd! How do I make this work for both types of URL?
I tried a rather different method to make this a generic redirect, that could be included even if the folder name wasn't project, but this has the same problem:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old\.domain\.tld$ [NC]
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule . https://domain.tld/subfolder/%1 [R=301,L]
In a server-wide configuration, a slash is appended to all requests for index files, e.g. old.domain.tld/project is redirected to old.domain.tld/project/. Could this be causing a problem?

htaccess redirect folder to a third level domain

I have a website https://www.mywebsite.it with a few pages in the root.
Then I have a folder named myapp where I have an application developed.
Actually the application myapp can be accessed via:
https://www.mywebsite.it/myapp
https://myapp.mywebsite.it
What I want to do is: if the user navigates to https://www.mywebsite.it/myapp apache will redirect him to https://myapp.mywebsite.it
Is something like:
RewriteEngine On
RewriteRule ^/?myapp/ https://myapp.mywebsite.it/? [R=301,L]
inside the vhost configuration file going to work?
Inside myapp/.htaccess you can use this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?(mydomain\.it)$ [NC]
RewriteRule .* http://myapp.%1/$0 [L,R=301,NE]

Redirect all pages in direcory except one subdirectory .htaccess

I want to have one URL still being served from a subdirectory on the old domain but redirect all others from the parent directory to the new domain.
www.olddomain.co.uk/directorya/directoryb -> no redirect
www.olddomain.co.uk/directorya -> www.newdomain.co.uk/directorya
This works if the URL is typed with a trailing slash
RewriteCond %{REQUEST_URI} !^/directorya\/directoryb [NC]
RewriteRule ^(.*)$ http://www.newdomain.co.uk/ [L,R=301]
So http://www.olddomain.co.uk/directorya/directoryb/ works but http://www.olddomain.co.uk/directorya/directoryb fails.
Tried added $ at the end and other connotations and tried
RewriteEngine on
RewriteRule !^myspecialdirectory/mynextdirectory($|/) http://newdomain.example%{REQUEST_URI} [L,R=301]
from Redirect entire site except one directory to a new site - apache .htaccess
What do I need to do to get http://www.olddomain.co.uk/directorya/directoryb/ or http://www.olddomain.co.uk/directorya/directoryb to redirect? (while all other http://www.olddomain.co.uk pages go to http://www.newdomain.co.uk
For reasons I don't understand http://www.olddomain.co.uk/directorya/directoryb goes to Google and doesn't redirect
Try using THE_REQUEST variable and make sure this this very first rule:
RewriteCond %{THE_REQUEST} !\s+/+directorya/directoryb [NC]
RewriteRule ^ http://www.newdomain.co.uk/? [L,R=301]
Test it after clearing your browser cache.

mod_rewrite prefix as a folder

I have this structure in my home directory:
/index.php
/.htaccess
/folderx/
/folderx/subfolder/
/folderx/subfolder/file.php
What I need, is to mod_rewite all requests with the prefix "folderx_" to the real folderx directory. But additionally, I need to force redirection - the user should't be available to request /folderx/subfolder/subsubfolder/file.php directly. It should be redirected to the mod_rewrite url: /folderx_subfolder/subsubfolder/file.php
/folderx_subfolder/ => /folderx/subfolder/
/folderx_subfolder/file.php =>
/folderx/subfolder/file.php
/folderx_subfolder/subsubfolder/ =>
/folderx/subfolder/subsubfolder/
/folderx_subfolder/subsubfolder/file.php
=> /folderx/subfolder/subsubfolder/file.php
I've tried the code below, but the redirect is not working properly. When requesting the subfolder, it redirects to the prefix, but I don't know how to prevent looping( Please, help!
/folderx/subfolder/subsubfolder/file.php
RewriteCond %{REQUEST_URI} ^/folderx/(.*)$
RedirectMatch ^/folderx/(.*)$ /folderx_$1
RewriteRule ^folderx_([a-z]{3,15})(/?)+$ /folderx/$1/ [NC]
RewriteRule ^folderx_([a-z]{3,15})/(.*)$ /folderx/$1/$2 [NC]
RewriteRule ^folderx/(.+)$ http://example.com/folder_$1 [nc,last,redirect]
RewriteRule ^folderx_(.+)$ /folderx/$1 [nc]