.htaccess not excluding sub directory with rules - php

I'm asking a question regarding my .htaccess. I'll quickly explain what i'm trying to accomplish and how it's not working.
I'm using the $_GET directory as a rule for the page.
i.e https://example.com/test?p=page1 = https://example.com/test/page1
I also have a directory called "account" inside this /test/ directory but when I try to access the page.
https://example.com/test/account it redirects me to https://example.com/test/account/?p=account
note (https://example.com/test/account/ works fine)
I've tried disabling it by following methods on other StackOverflow posts but none of this is working.
My rule is:
RewriteRule ^([0-9a-zA-Z-_\ ]+)/?$ index.php?p=$1 [NC,L]
Is there any way I can configure this rule to exclude the word "account" and "admin" etc I've tried placing a .htaccess with the rewrite engine off but that doesn't work because the .htaccess thinks /account is a page rather than a directory.
I've tried other methods which stop directories with the same issue. It doesn't think /account is a directory. It thinks it's a page.
Any help would be great!

add this line above to exclude requests with account or admin in them
RewriteCond %{REQUEST_URI} !(account|admin)$ [NC]
put that above your current rule and it should exclude using the rule on requests including those terms.
here is what I use (have for ages), to do what you are wanting to do:
**I am pretty sure I could reduce this to a single set of expressions but I'm not that good. Without the second I miss domain.tld and without the first I miss fancy.domain.tld
ErrorDocument 404 http://domain.tld
RewriteEngine On
RewriteCond %{REQUEST_URI} !(actionspage|\/excludeddir\/|\/otherdir\/).*$ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.tld$ [NC]
RewriteRule ^(.*)$ index.php?pre=%1&pos=$1 [L,QSA]
RewriteCond %{REQUEST_URI} !(actionspage|\/excludeddir\/|\/otherdir\/).*$ [NC]
RewriteRule ^(.*)$ index.php?pre=0&pos=$1 [L,QSA]

Related

htaccess forward to subfolder but hide subfolder name

I have an issue writing some regex to go inside my htaccess file.
Basically, my site has been setup so that index.php and all other site files are not in the root (public_html) directory but instead are in http://fitnessquiz.co.uk/fitnessquiz.co.uk/
Initially I tried the following in my public_html folder:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^fitnessquiz.co.uk$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.fitnessquiz.co.uk$
RewriteCond %{REQUEST_URI} !fitnessquiz.co.uk/
RewriteRule (.*) /fitnessquiz.co.uk/$1 [L]
which correctly navigates to my homepage and displays the url correctly but then when I click any link I get a "no input file specified" message. So then I tried replacing with:
RewriteCond %{REQUEST_URI} !^/fitnessquiz.co.uk/
RewriteRule ^(.*)$ /fitnessquiz.co.uk/$1 [L,R=301]
After which the site works but every url looks like this:
http://fitnessquiz.co.uk/fitnessquiz.co.uk/someotherfolder/etc.php
I've tried various htaccess regex solutions listed elsewhere on here but none seem to work, how do I accomplish both of these things i.e. redirect to /fitnessquiz.co.uk for every url but hide the duplicate url name/folder. Im on a shared server so don't have permissions to change any server/apache settings directly.
According to this answer by nuked on a previous post you could try:
RewriteCond %{THE_REQUEST} ^GET\ /fitnessquiz.co.uk/
RewriteCond %{HTTP_HOST} ^(www\.)?fitnessquiz.co.uk$
RewriteRule ^fitnessquiz.co.uk/(.*) /$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?fitnessquiz.co.uk$
RewriteRule !^fitnessquiz.co.uk/ fitnessquiz.co.uk%{REQUEST_URI} [L]
This set of rules worked for me on a very similar situation. I had employed the same cure, (re-direct if calling the folder and hide after re-writing it) but I never got the order right on my own. Thus I kept seeing the page not found errors too. Below is my attempt to explain the actions, for my own learning, hopefully others too.
RewriteCond %{THE_REQUEST} ^GET\ /fitnessquiz.co.uk/
Is asking the question, does THE_REQUEST contain the subfolder you need to hide?
RewriteCond %{HTTP_HOST} ^(www\.)?fitnessquiz.co.uk$
Checks if the request is for the correct host.
RewriteRule ^fitnessquiz.co.uk/(.*) /$1 [L,R=301]
Rewrite the URL as one without the subfolder and call the new link in the redirected browser. Note:
L : Last step. Stop processing other rules
R=301 : After re-writing, redirect the browser to the new URL.
When the page is redirected it has no subfolder so the first RewriteRule is skipped. And then
RewriteCond %{HTTP_HOST} ^(www\.)?fitnessquiz.co.uk$
checks if calling the right host. And then
RewriteRule !^fitnessquiz.co.uk/ fitnessquiz.co.uk%{REQUEST_URI} [L]
rewrites the url that has not the subfolder to use the correct subfolder without redirecting the page, and while hiding actual subfolder from the browser. Again note:
L : Last step. Stop processing other rules

Specific redirection in .htaccess does not work

I have a specific problem with my mod_rewrite configuration that I cannot resolve. I am no admin, therefore I'm kindly asking for a collective advice :) Please note - it's not a general question about redirection, but very specific one.
Story
I have a shared hosting with access to FTP and ability to create my own .htaccess files. This shared hosting had plenty of files and directories before I created the website, so logical step for me was to place everything inside new-site folder.
Then I had to create custom rewrite rules so that everything under example.com points to new-site.
CONFIG
So I came up with the following config.
# (...) other rules
# 1. Make sure that /new-site/ is not a duplicated content
RewriteCond %{REQUEST_URI} ^/new-site/
RewriteRule ^/new-site/(.*)$ /$1 [R=301,L]
# 2. Make sure that example.com is internally handled by files in '/new-site'
RewriteCond %{REQUEST_URI} !^/new-site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /new-site/$1
RESULTS
Rule marked with 2. works fine, my site is accessible as I want. However I didn't want https://example.com/new-site/ to be found on the server by bots and treated by a duplicated content, so I added rule 1..
This rule, however, doesn't seem to have any effect! I looked it up with CURL and request is handled immediately with a 200 status. I'm banging my head against the wall and experimenting with other variants of it, but everything fails.
What I'm after is pretty darn simple:
Make every request to the root domain be handled by website which is stored in /new-site/
Make sure that direct call to https://example.com/new-site/(.*) is redirected with 301 status back to the domain root.
What am I doing wrong?
EDIT
I've noticed that my setup seems to be doing far better if I remove a child .htaccess file under /new-site/ subfolder. I didn't mention it in my original question because there is nothing special about it (just some SEO rewrites).
RewriteEngine on
DirectoryIndex index.php
RewriteRule ^products$ products.php
# (...) similar rewrites
Old answer: RewriteRule does not accept leading slash. Try to change to
RewriteRule ^new-site/(.*)$ /$1 [R=301,L]
Edit:
Version that is provided by you will forward to the cyclic redirection. To avoid it, I think, you can use such .htaccess
RewriteEngine on
RewriteRule ^new-site/ - [R=404,L]
# 2. Make sure that example.com is internally handled by files in '/new-site'
RewriteCond %{REQUEST_URI} !^/new-site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /new-site/$1 [L]
Direct asking /new-site/* wil receive 404 error, while url exaple.com/* wil be redirected to /new-site
And notice that if there are files with the same name, for example, /r.jpg and /new-site/r.jpg, the last never be achieve
Your first rule never matches because it must not begin with a leading slash.
With RewriteRule, you only need a leading slash if you're directly in httpd.conf or before Apache v2.4 i think.
While you have a good idea, your first rule will cause an infinite redirection loop if it's working. You have to use THE_REQUEST to match direct user request only.
You can put this code in /.htaccess
# 1. Make sure that /new-site/ is not a duplicated content
RewriteCond %{THE_REQUEST} \s/new-site/([^\s]*)\s [NC]
RewriteRule ^ /%1 [R=301,L]
# 2. Make sure that example.com is internally handled by files in '/new-site'
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!new-site/).*)$ /new-site/$1 [L]
Also, you'll have to add this line in /new-site/.htaccess (to avoid automatic override)
RewriteOptions InheritBefore

RewriteRule not being tiggered on subfolders

I have [maybe] an unusual setup:
I have 1 storage/root folder with several domain and subdomains all
pointing to the same folder.
I then create a subfolder for each domain or subdomain and us
.htaccess to redirect the domain to the correct folder/files
Below is a working example of how i do this
# Domain1.com
RewriteCond %{HTTP_HOST} ^spain\.domain1\.com [NC]
RewriteRule ^(.*)$ /spain/$1 [L]
This all works fine, HOWEVER:
If a user goes to doamin1.com/spain, they are of course presented with all files in that folder...
SO, i try to add a redirect just on the domain1.com/spain... yet this does not work... i have tried everything. including trying the redirect for the root .htaccess and the spain/.htaccess. Neither work.
I am trying the below (does not work)
# Domain1.com/spain
RewriteCond %{HTTP_HOST} ^domain1\.com/spain/ [NC]
RewriteRule (.*)$ http://www.domain1.com/? [L]
I have tried may variations of the above, but none seem to work (at all)
However the RedirectMatch does work (below) but also breaks the above subdomain (in the first example).
RedirectMatch ^/spain/$ /
if i try to match on more and add the RedirectMatch as (below) this then does not trigger the match...
RedirectMatch ^www\.domain1\.com/spain/$ /
I know what i need to do is possible (i think and assume), but im banging my head on walls here.....
PLEASE advise and of course thank you!!!
This is not correct:
# Domain1.com/spain
RewriteCond %{HTTP_HOST} ^domain1\.com/spain/ [NC]
RewriteRule (.*)$ http://www.domain1.com/? [L]
Because that last part (/spain) is NEVER part of the HTTP_HOST so it will never match :)
What you want to do is something like this
# Domain1.com/spain
RewriteCond %{HTTP_HOST} ^domain1\.com [NC]
RewriteRule ^spain(.*)$ http://www.domain1.com/? [R=302,NC,L]
In other words, to move the /spain part of the URL in RewriteRule where it is actually being matched and keep the domain name match in RewriteCond above.

I'm having problems with RewriteRule

I have a site I'm trying to develop and I am having some (probably noob) problems getting my reWrite rules to work correctly.
Basically, I'm using the codeIgniter framework to develop the site and as part of this I have created a RewriteRule to remove the index.php part of the URL with the exception of some folders for images etc.
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|shop)
RewriteRule ^(.*)$ /TESTSite/index.php/$1 [L]
The site originally has a current page of page/facilities/our-shop which was a very basic page with a few links to paypal to make a very rough shop page as we needed one quickly. However now I have implemented a different (read OpenCart) shop section to make this a bit more professional and allow more features etc. I have placed the openCart in a subdirectory called shop. The shop continues to work correctly as I've excluded the /shop from my original reWrite rule.
However - My problem...
I want the old page page/facilities/our-shop to redirect to the /shop and therefore work as if I navigated directly there..
I have tried the following:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|shop)
RewriteRule ^page/facilities\/our-shop$ /TESTSite/shop/$1 [R,NC,L]
RewriteRule ^(.*)$ /TESTSite/index.php/$1 [L]
And
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|shop)
RewriteRule ^page/facilities\/our-shop$ /TESTSite/shop/$1 [N]
RewriteRule ^(.*)$ /TESTSite/index.php/$1 [L]
And with a few other variations, but every time I can't get it to work. Can anyone advise please?
Thanks
I think you can try the following rules:
RewriteEngine on
RewriteRule TESTSite/page/facilities/our-shop$ /TESTSite/shop/$1 [R=301]
RewriteCond %{REQUEST_URI} !^(index\.php|images|css|js|robots\.txt|shop)
RewriteRule ^TESTSite/page/(.*)$ /TESTSite/index.php/$1 [L]
Two things:
When you use RewriteCond, it only applies to the following rule. So I re-ordered the two lines.
You do not need \ before a / in a rule.
Check this tool to debug your rules.

Redirect to another folder

I read this .htaccess rewrite to redirect root URL to subdirectory and i'm trying to achieve the same things.
The solution with most up votes was:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^$ store [L]
Now, looking at the comments it seems to be working good.
The problem is that I'd like not to hardcode any path (as instead provided above "www.example.com"). I'd like that my .htaccess inside projectname/ redirects to projectname/public/ despite what the real server host is. So that if I put projectname/ inside the root server of www.pinco.com it redirects to www.pinco.com/projectname/public/ but it shows www.pinco.com/projectname/.
How can I achieve that?
The example you found is actually doing two different things.
# This is actually just redirecting all http://example.com/somepage/
# to http://www.example.com/somepage/, to ensure all URLs have the www.
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
The second rewrite is what will help you achieve what you're trying to do.
# This redirect all requests for http://example.com -> http://example.com/newdir
# If you are looking to redirect the request, so the URL contains directory name,
# you can change the [L] to [R=301,L]
RewriteRule ^$ /newdir [L]
# If your concerned about direct access to a particular page without the sub-dir
# you will want to add something like this
RewriteCond %{REQUEST_URI} !^/newdir
RewriteRule (.*) /newdir$1 [R=301,L]
So, in that case, you won't have to be concerned with domain that the application is running on.
try to add this line, and :
RewriteBase /projectname
And you stay inside the project.

Categories