.htaccess serving subfolder at from url of another subfolder - apache

My folder structure looks like this:
/.htaccess
/admin
/admin/api
/admin/admin
index.html (Angular JS app)
So I need to do the following rewrites:
Serve /admin/admin at /admin
Serve /admin/api/* at /admin/api/* (no rewrite)
Serve /{everything else} from index.html
How can I do this in the root .htaccess?
So far, I've got:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L]
</IfModule>
But this doesn't handle the rewriting of /admin/admin to /admin. Can anyone help with this?

You can use:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^admin/?$ admin/admin [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(index|admin)
RewriteRule ^ index.html [L]
</IfModule>
This is assuming there is no .htaccess inside /admin/ folder. In case there is then use this rule there:
RewriteEngine On
RewriteBase /admin/
RewriteRule ^/?$ admin [L]

Related

htaccess redirect to index.html in same (unknown) subfolder

I like to redirect all request to index.html where the .htaccess is placed
Place of .htaccess:
https://example.com/unknown/whatever/.htaccess
This request:
https://example.com/unknown/whatever/article1
should redirect to
https://example.com/unknown/whatever/index.html
It only works for me, when i know the subfolder path:
RewriteEngine On
RewriteBase /
DirectorySlash Off
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /unknown/whatever/index.html [L]
Any idea how to make this work when the subfolder path and count is unknown?
It works for me (requested URL - unknown/whatever/article1)
RewriteEngine On
RewriteBase /
DirectorySlash Off
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)$ $1/index.html [L]
server files structure:
unknown/whatever/article1/index.html
.htaccess
and this one works for me too (requested URL - unknown/whatever/article1):
RewriteEngine On
RewriteBase /
DirectorySlash Off
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)/(.*)$ $1/index.html [L]
server files structure:
unknown/whatever/index.html
.htaccess
request: unknown/whatever/article1
.htaccess:
RewriteEngine On
DirectorySlash Off
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)$ index.html
Files structure:
unknown/whatever/index.html
unknown/whatever/.htaccess

Unable to remove index.php from Codeigniter 3.0 URL

I am unable to remove index.php from CodeIgnitor 3.0 URL. I have applied so many solutions but nothing worked. I am using WAMP 2.5 . rewrite_module is active in Apache . $config['index_page'] = ''. My htaccess file which is in the root folder is as follows
RewriteEngine on
RewriteBase /Reporting/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|js|img|css|captcha|robots\.txt)
RewriteRule ^(.*)$ /yourfolder/index.php/$1 [L]
Reporting is the folder in www where all the files are there.
I had problems with removing index.php with the default .htaccess code, but I managed and this is how my .htaccess looks like now.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
<Files "index.php">
AcceptPathInfo On
</Files>
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
Try with this code in .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
You have this in Codeigniter documentation - READ

Redirect to index.php of .htaccess dir

In .htaccess code is written as
RewriteEngine On
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)?$ /project/index.php/$1 [L]
But this .htaccess is redirecting to index.php of my web root rather than that of the project. I have server which have wordpress installed directly at root (with its own .htaceess) and I have to get the project directory working with that . .htaccess of wordpress is written as below but I dont have access to edit it .
SetEnv PHP_VER 5_TEST
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{QUERY_STRING} !lp-variation-id
RewriteRule ^go/([^/]*)? /wp-content/plugins/landing-pages/modules/module.redirect-ab-testing.php?permalink_name=$1 [QSA,L]
RewriteRule ^landing-page=([^/]*)? /wp-content/plugins/landing-pages/modules/module.redirect-ab-testing.php?permalink_name=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
How can I force to redirect to index of my project from my .htaccess
Try this code in /project/.htaccess:
DirectoryIndex index.php
RewriteEngine On
RewriteBase /project/
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [L]

strange redirect with url rewrite

My .htaccess file is:
RewriteEngine on
RewriteRule (.*) index.php?_url=$1 [QSA,L]
It works perfectly in most cases. But if the requested url exists as a real folder, it goes crazy.
For example when i request /random it is rewritten to index.php?_url=random with status code 200, but for /static (which is an existing directory on the web server) it gives a 301 redirect to /static/?_url=static
use this below code for .htaccess
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
If you want to exclude existing files and directories from being rewritten, do so with a RewriteCond
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?_url=$1 [QSA,L]

htaccess to rewrite wordpress subdirectory with permalinks to root

I have a wordpress site installed in the directory "wp". I want to keep it in that folder for maintenance purposes. Wordpress gives the option to create a htaccess for permalinks. You can see that code (which works) below.
What I want is a rewriterule so - to visitors - the site appears to be in the root. I've to change the rewritebase, but that didn't work.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wp/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wp/index.php [L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wp/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wp/index.php [L]
</IfModule>
1) in your dashboard, go to settings -> general and make sure
a) wordpress directory -> http://mydomain.com/wp
b) site address -> http://mydomain.com
2) move your index.php from subdirectory to the root (MOVE, don't just copy)
3) edit your index.php to read
/** Loads the WordPress Environment and Template */
require('./wp/wp-blog-header.php');
where "wp" is your subdirectory
4) delete any .htaccess file in the subdirectory
5) add this to your .htaccess in the root
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Under this setup, your wordpress installation will be located in /wp/ directory. Visitors will visit your site using http://mydomain.com. Good luck.
What would you think of that :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /wp/index.php [L]
RewriteCond %{REQUEST_URI} !^/wp/.*$
RewriteRule ^(.*)$ /wp/$1 [L]
You rewrite all non existing things to /wp/index.php, and all non /wp/ file to a /wp/ equivalent file. So if you call index.php it's in fact /wp/index.php
For a site with two WordPress blogs, one at the site root, and one in a subfolder, i.e. https://example.com/ and https://example.com/otherblog/, here's what i did
<Directory /data/sites/example.com/otherblog>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /otherblog/index.php [L]
</IfModule>
</Directory>
<Directory /data/sites/example.com>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
</Directory>