htaccess in subfolder cant rewrite to file - .htaccess

In the root directory there's an htaccess file that directs everything that lands on it to goto a diff domain
RewriteEngine on
RewriteRule (.*) http://www.domain.com/
http://www.domain.com/123 = http://www.domain.com/
I have a folder named 'admin' within the root dir with another htaccess document inside it, I want that htaccess doc to point the root of itself to 'admin.php'
RewriteEngine on
RewriteRule ^$ /admin.php
http://www.domain.com/admin/ = http://www.domain.com/admin/admin.php
But no matter how I do it, I either get 500 Error or the rewrite rule from the main root doc over-rides it.
Hope that makes sense, thanks.

The easiest way to fix this (there are several) is probably exclude the admin directory from the RewriteRule in the root using a condition like this:
RewriteEngine on
RewriteCond $1 !^admin/.*
RewriteCond $1 !^admin\.php
RewriteRule (.*) http://www.domain.com/

Related

Forcing to append text to the end of a file name with .htaccess

Currently i have this directory:
example.com/curriculums/34-william.pdf
example.com/curriculums/168-romina.pdf
34-william.pdf and 168.romina.pdf are actual files, and they do exist, but they should only be accesible if accessed using these urls:
example.com/curriculums/34-william-cv.pdf
example.com/curriculums/168-romina-cv.pdf
So that:
If a visitor opens example.com/curriculums/34-william.pdf it should return a 404 response code
If a visitor opens example.com/curriculums/34-william-cv.pdf the server should return the contents of example.com/curriculums/34-william.pdf
Any ideas? Thanks!
Assuming you don't have any .htacecss inside curriculums directory, you may use these rules inside your site root .htaccess:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^curriculums/[\w-]+(?<!-cv)\.\w+$ $1$2 [L,NC,R=404]
RewriteRule ^(curriculums/[\w-]+)-cv(\.\w+)$ $1$2 [L,NC]

How to always redirect to subfolder using htaccess

I want to redirect visitors who are viewing index (not other files) to a sub folder in every conidition. At the moment I'm using the code below in my .htaccess file:
<IfModule mod_rewrite.c>
RewriteRule ^$ /dir [L,R=301]
</IfModule>
But it only works on the main domain but doesn't work when we have the script itself in a subfolder. How should I modify this code to works everywhere?
The previous code works like below when installed on the root folder:
http://www.domain.com => http://www.domain.com/dir
Which is fine, but when we have it on sub, it works like below:
http://www.domain.com/sub => http://www.domain.com/dir
That means it move one level up and then locate for dir and that's the failure.
Use that:
# Change in subfolder:
RewriteBase /sub/
RewriteRule ^$ dir [L,R=301]
In root .htaccess for each subfolder:
RewriteRule ^([^/]+)/$ $1/dir/ [L,R=301]

htaccess redirect if filename equals something

I'd like to set a redirect (preferably with RewriteCond) so that if the requested file is index.php regardless of directory, it will be redirected to another site.
So visiting /index.php or/files/index.php or /stuff/index.php (etc.) will all redirect you to another domain.
Here is a general way to do it. This rule set should be placed in the .htaccess file in the root directory:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} index\.php/? [NC]
RewriteRule .* http://otherdomain.com/ [R=301,L]
Redirects permanently any URL that holds index.php in any position, like
http://mydomain.com/index.php or
http://mydomain.com/any/folder/quantity/index.php or
http://mydomain.com/any/folder/quantity/index.php/any/folder/quantity/
To
http://otherdomain.com/
That's it. You don't explain much so nothing is passed to the other domain, just as you say in your question:
...redirected to another site.
These rules should do it (when placed inside /.htaccess file):
RewriteEngine On
RewriteRule (?:^|/)index\.php$ http://otherdomain.com/ [R=301,L]

subdomain including htaccess redirect

I have a subdomain code.domain.com that redirects to domain.com/code
I want to redirect for example: code.domain.com/hello/world.html to domain.com/code/?path=hello/world.html
This is what I have so far:
RewriteEngine ON
RewriteRule ^code/(.+)$ http://domain.com/code/?path=$1
But I am not sure what is wrong with it, it doesn't seem to work.
Thanks.
Use these rules in the root directory htaccess file (not the code directory) (you can also point the code.domain.com to the root directory instead if you want) :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^code.domain.com$
RewriteRule ^(.+)$ /code/?path=$1

Virtual subdomain with wildcard mapping .htaccess

I am building a download site, An users will be able to register and a folder with their username will be created on my server, something like: home/users/username
What I want to accomplish is, if anyone types: username.domain.com in their browser, it will route them to: home/users/username/, and if they type: username.domain.com/file.mp3, it will route them to: home/users/username/file.mp3
If its possible to accomplish sub folders routing, that would be great full aswell, example; home/users/username/sub/file.mp3
Thanks guys
Try adding the following to the .htaccess file in the root directory of your site.
This will rewrite any request to username.domain.com to the correct folder/subfolder and file.
I am assuming that home is a directory in the root folder of your site.
RewriteEngine on
RewriteBase /
#if request for usename.domain.com/anything
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
#send any request that is not already for home/users to home/users/username/anything
RewriteRule ^(?!home/users/) home/users/%1%{REQUEST_URI} [L,NC]
I tried what Ulrich Palha recommended but no luck. I hope this will help guys like me where Ulrich's answer doesn't work.
# Rewrite username.domain.com/<path> to domain.com/home/username/<path>
#
# Rewrite only if not already rewritten to /home/
RewriteCond $1 !home/
# Skip rewrite if subdomain is www
RewriteCond %{HTTP_HOST} !^www\. [NC]
# Extract (required) subdomain to %1
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$
# Rewrite to /home/username/URL-path
RewriteRule (.*) /home/%1/$1 [L]

Resources