My webroot has two directories. One is called 'functions', and the other is called 'site'. I have added an '.htaccess' to the webroot along with the other two directories. There can be two types of incoming requests, request to view a page or request to use an api.
Now, I want a check on the .htaccess that checks if the request_uri contains '/api/' in it. If it contains, the request needs to be forwarded to the 'functions' directory. For every other request (i.e. any request_uri that does not contain '/api') the request needs to be forwarded to the 'site' directory.
I await help writing this.
Very simple. Add the following to your /.htaccess :
RewriteEngine on
#if Request_uri string contains "/api/" forward the request to /function dir
RewriteCond %{REQUEST_URI} /api/ [NC]
RewriteRule ^.+$ /function%{REQUEST_URI} [L]
# else redirect the request to /site directory
#redirect all other requests except /site and /api to /site directory
RewriteCond %{REQUEST_URI} !/(site|api) [NC]
RewriteRule ^.*$ /site%{REQUEST_URI} [L]
In the second Rule we excluded /site directory from the rule to avoid rewriting /site/ requests to /site .
Note : The second rule also rewrites your homepage / to /site .If you don't want to redirect your home directory then simply change the Rule's pattern to ^.+$ .
Related
I have assets[/admin/assets] directory which I restricted access from browser.
So whenever user requested on [/admin/assets] , I want to rewrite rule to [admin/index.html].
I tried with below setting , but this doesn't rewrite to expected path but show permission access error with 403.
RewriteRule /assets /index.html [L]
I have one solution for that by handling ErrorDocument. But I don't prefer it that way, I want to handle by RewriteRule .
This can be achieved using the mod_rewrite module in Apache web server. You can add the following code in your .htaccess file or server configuration file to rewrite the URL:
RewriteEngine on
RewriteCond %{REQUEST_URI} /assets
RewriteRule ^assets/(.*)$ /admin/index.html [L]
The RewriteEngine directive turns on the rewriting engine.
The RewriteCond directive specifies a condition that must be met before the RewriteRule is applied. In this case, the condition checks if the requested URL starts with "/assets".
The RewriteRule directive specifies the actual URL rewriting. The pattern "^assets/(.)$" matches any URL that starts with "/assets/", and captures the rest of the URL into a group (.). The URL is then rewritten to "/admin/index.html". The [L] flag specifies that this is the last rule to be applied, so no further rewriting should take place.
I set by Directory module with Require All Denied
You can't block access and rewrite the request. The <Directory> container will take priority. (But there's no point blocking the request when you want to rewrite it. You are essentially blocking it by rewriting.)
Remove the <Directory> block and directly inside the <VirtualHost> container (outside of any <Directory> section) use mod_rewrite:
RewriteEngine On
RewriteRule ^/admin/assets($|/) /admin/index.html [L]
Any requests to /admin/assets or /admin/assets/<anything> are internally rewritten to /admin/index.html.
I have 5 URLs on a subdomain website http://subdomain.example.com, and I want them to redirect to 5 other URLs on my main website, https://www.example.com/.
Important: URLs do NOT have the same structure!
Example:
http://subdomain.example.com/url1 should redirect to https://www.example.com/ipsum
http://subdomain.example.com/url2 should redirect to https://www.example.com/lorem
etc.
How can I handle that?
UPDATE:
There is a play folder (name of the subdomain) which contains the subdomain website files and a htdocs folder which contains the www website files.
Here is the .htaccess file in my play folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Since the subdomain has it's own .htaccess file, you don't need to specify the hostname as part of the redirect. And since you already have mod_rewrite directives in the subdomain's .htaccess file, you should also use mod_rewrite for these redirects (to avoid conflicts). Otherwise, you'll need to specify these redirects one-by-one.
Try the following at the top of your subdomain's /play/.htaccess file. Note that this needs to go before the existing directives in the file.
# Specific redirects
RewriteRule ^url1$ https://www.example.com/ipsum [R=302,L]
RewriteRule ^url2$ https://www.example.com/lorem [R=302,L]
The above would match a request for http://subdomain.example.com/url1 and redirect accordingly, etc.
Note that the RewriteRule pattern (regular expression) does not start with a slash when used in a per-directory (.htaccess) context.
Note that these are 302 (temporary) redirects. Change them to 301 (permanent) - if that is the intention - only once you have confirmed they are working OK (to avoid caching issues).
Try this...
Redirect 301 http://subdomain.example.com/url1 https://www.example.com/ipsum
Redirect 301 http://subdomain.example.com/url2 https://www.example.com/lorem
we have created a CDN subdomain to host images, using this URL:
https://cdn.example.com/images/
What we want is to redirect, if someone goes to any other path of this subdomain. For example:
https://cdn.example.com/
https://cdn.example.com/blabla/
Redirect to other domain, just not redirect if the folder is images.
create htaccess file in your document root, and deny direct access to all files
Order deny,allow
Deny from all
and create htaccess file in images:
Allow from all
Try the following using mod_rewrite, near the top of your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^cdn.example.com [NC]
RewriteRule !^images/ https://example.com%{REQUEST_URI} [R,L]
If you access cdn.example.com/<something> and the URL-path does not start /images/ then redirect to https://example.com/<something> (the main domain).
Change R to R=301 (after you've made sure it is working OK) if you want a permanent redirect.
UPDATE: To exclude additional folders (eg. images2 and images3) then you can change the RewriteRule directive to:
RewriteRule !^(images|images2|images3)/ https://example.com%{REQUEST_URI} [R,L]
If the folders are literally called images, images2 and images3 then this could be simplified to match (or rather not match) any URL of the form /imagesN - where N is an optional digit:
RewriteRule !^images\d?/ https://example.com%{REQUEST_URI} [R,L]
I have directory structure like this:
public_html
.htaccess[1]
-apps
-.htaccess[2]
-admin
-myviwo
when I request http://localhost/mysite/admin it redirect me to http://localhost/mysite/apps/admin/ and shows me the content of the admin directory, if I request http://localhost/mysite/admin/ it doesn't redirect me but it shows me the content of admin directory again which is correct. But I want:
http://localhost/mysite/admin
http://localhost/mysite/admin/
Both of the above URLs shows me the content of admin directory without redirecting me.
.htaccess [1]
RewriteEngine On
RewriteBase /
RewriteRule (.*) apps/$1 [L]
.htaccess [2]
RewriteEngine On
RewriteBase /
RewriteRule ^admin/?(.*)$ admin/$1 [L]
RewriteRule ^(.*)$ myviwo/$1 [L]
How can I achieve this?
In the admin directory, add an htaccess file with the following:
DirectorySlash Off
This makes it so mod_dir won't redirect requests for the directory admin. However, note that there's an important reason why directory slash is "on" by default:
Security Warning
Turning off the trailing slash redirect may result in an information disclosure. Consider a situation where mod_autoindex is active (Options +Indexes) and DirectoryIndex is set to a valid resource (say, index.html) and there's no other special handler defined for that URL. In this case a request with a trailing slash would show the index.html file. But a request without trailing slash would list the directory contents.
If that's ok with you, then that's all that you need.
Otherwise, you may need to add a special rule specifically for admin. In the .htaccess[1] file, add right below the rewrite base:
RewriteRule ^admin$ apps/admin/ [L]
EDIT: to make the above rule dynamic, you need to first check if it's a directory:
RewriteCond %{DOCUMENT_ROOT}/apps%{REQUEST_URI} -d
RewriteRule ^(.*[^/])$ apps/$1/ [L]
trying to rewrite URL strings only for pages that reside in the root, removing the page extension for cosmetic reaons. For example:
www.site.com/page.html ==> www.site.com/page
www.site.com/about.html ==> www.site.com/about
Using this code currently:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^.]+\.com) [NC]
RewriteRule (.*) http://www.%1/$1 [R=301,L]
BUT, I don't want the rule to modify sub-directiories or sub-domains. For example I also have:
clientA.site.com (which is mapped to www.site.com/clientA), which I need to not be remain unchanged. Right now it is sending that page (client.site.com/index.html) to a page not found.
Your rule doesn't affect subdomains. There must be other rules causing the 404 not found response.
If you want to restrict requests to the root pages, you can specify that with an appropriate pattern by excluding slashes in the request URL path
# exclude directories
RewriteCond %{REQUEST_FILENAME} !-d
# allow top level request URLs only
RewriteRule ^[^/]*$ http://www.%1/$0 [R,L]
Never test with 301 enabled, see this answer
Tips for debugging .htaccess rewrite rules
for details.