Rewrite request to a sub directory - php

I have a web application in a directory suppose xyz. I am trying to redirect all requests to a sub directory www. My current .htaccess code is:
RewriteEngine On
RewriteRule (.*) /www/$1 [L]
But, when I visit http://example.com/xyz/some_url the request is redirected to http://example.com/www/some_url rather http://example.com/xyz/www/some_url which I want.
Update: Sorry, I forgot to mention the directory xyz as in my case is likely to renamed. So, this directory doesn't need to be hard coded in the rewrite rule

Place this rule in /xyz/.htaccess:
RewriteEngine On
RewriteBase /xyz/
RewriteRule ^((?!www/).*)$ www/$1 [L,NC]

If your DocumentRoot does not contain the xyz path then you'll need to specify in the RewriteRule.
E.g.: RewriteRule (.*) /xyx/www/$1 [L]

This should work or be close I think:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/([^/]+)$ $1/www/$2 [L]

Use a relative path and two .htaccess files
You need only remove the leading / to rewrite to a url relative to the current folder:
# /xyz/.htaccess
RewriteEngine on
RewriteRule (.*) www/$1 [L]
This will rewrite /xyz/foo to /xyz/www/foo.
To prevent redirect loops add a .htaccess file to the www subfolder e.g.:
# /xyz/www/.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Adjust as appropriate.

Related

How to redirect all requests within a subfolder

I have a website called mydumbwebsite.com/. In my root folder, I have various folders, one of them being "stuff". This folder is directly accessible with mydumbwebsite.com/stuff/. I want to create an .htaccess file that redirects everything that goes into this subfolder and only this subfolder, so these urls:
mydumbwebsite.com/stuff/test
mydumbwebsite.com/stuff/test/
mydumbwebsite.com/stuff/test.php
mydumbwebsite.com/stuff/test.php?test=yes
mydumbwebsite.com/stuff/test2.php (doesn't exist)
mydumbwebsite.com/stuff/test2.php?test=yes (doesn't exist)
mydumbwebsite.com/stuff/test/moar
mydumbwebsite.com/stuff/test/moar/tests.php
... should all redirect to the index.php file of the folder "stuff", even if the file/folder they point to exists.
Some additional context: despite my made up example, I encountered this problem on localhost. I have many different projects and I don't want the .htaccess of one project interfere with the other projects. I tried this, but it keeps redirecting me to the xampp homepage:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php [QSA,L]
I tried changing it to this, but that couldn't cover all instances, such as nr. 2, 3 and 4:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /stuff/index.php [QSA,L]
You need to remove the conditions !-d and !-f, which say "Don't apply the rule on existing files and directories". You also need to remove RewriteBase /.
RewriteEngine On
RewriteRule ^(.+)$ index.php [QSA,L]
The file should be located under /suff/.htaccess.
The following rules do also work for me:
RewriteEngine On
RewriteRule ^ index.php [L]
and
RewriteEngine On
RewriteRule ^ index.php [END]
Regarding your comment:
Usually I would create a public folder and put anything that should be accessed directly (public/css/, public/js, public/img etc.) in it. And whitelist it as following:
RewriteEngine On
RewriteRule ^public/ - [END] # allow direct access on public folder
RewriteRule ^ index.php [END] # anything else will be directed to index.php
You can of course whitelist multiple directories the same way:
RewriteEngine On
RewriteRule ^css/ - [END]
RewriteRule ^img/ - [END]
RewriteRule ^js/ - [END]
RewriteRule ^ index.php [END]
Or something like you suggested in the comment:
RewriteRule ^(scripts|styles|morefoldernames)($|/) - [L]
It is late, so my brain could be muddled about this, but surely you simply want everything in stuff to be redirected to index.php, no if's or buts.
Then the below should work, provided it has "/stuff/" in the URL. I'm guessing that you aren't bothered about query strings etc either, if so, then you'll need to modify the index.php below to index.php?$1 and the flags to [QSA, NC, L].
RewriteEngine On
RewriteRule ^/stuff/?$ /stuff/index.php [NC,L]
Check that your root .htaccess file has the line "RewriteOptions InheritDown" somewhere in it, that way the .htaccess file for each subfolder should be covered.
Try this out.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/stuff/(.+)$ /stuff/index.php [L]

URL Rewriting (Internal/External Forwarders) .htaccess

I am using .htaccess for URL Rewriting. My current .htaccess code is:
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+fr/transporter/transporterPublicProfile\.php\?profil=([^\s&]+) [NC]
RewriteRule ^ fr/profil-des-transporteurs/%1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^fr/profil-des-transporteurs/([^/.]+)/?$ fr/transporter/transporterPublicProfile.php?profil=$1 [L,QSA,NC]
My URL is :
https://example.com/fr/profil-des-transporteurs/1927
It works fine when I place .htaccess file in DOCUMENT_ROOT directory.
But it doesnot work when I put same .htaccess file in real sub-directory i.e /fr/.
So what should I do, if I want to place .htaccess file in sub-directory i.e /fr/
Use these rules inside /fr/.htaccess:
RewriteEngine On
RewriteBase /fr/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /fr/transporter/transporterPublicProfile\.php\?profil=([^\s&]+) [NC]
RewriteRule ^ profil-des-transporteurs/%1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^profil-des-transporteurs/([^/.]+)/?$ transporter/transporterPublicProfile.php?profil=$1 [L,QSA,NC]
Try this
RewriteEngine On
RewriteBase /fr

.htaccess WordPress ignore directory and remove index.php from that directory

So I know how to tell WordPress how to ignore a directory using the .htaccess file but I want to be able to remove the index.php from inside that directory too so I can access like this:
wordpresssite/directory/controller/function = wordpresssite/directory/index.php/controller/function
I've been trying but I'm clearly not very good at writing .htaccess files.
Thanks
My .htaccess file
The wordpress site is called smartronic and the subdirectory I want to ignore and remove index.php from is /mailin
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /smartronic/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !^/(mailin|mailin/.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /smartronic/index.php [L]
</IfModule>
# END WordPress
At the moment going to smartronic/mailin takes me to the xampp root page or a 404 page
To insert index.php into a fixed URL, you can use a simple RewriteRule
RewriteRule ^mailin/controller /mailin/index.php/controller [L]
If controller is variable, you need to capture this part with (...) and append it after the substitution with $1
RewriteRule ^mailin/(.*)$ /mailin/index.php/$1 [L]

Rewrite all URLs to index with path as GET variable

I am trying to forward all requests for other index.php files to the root index.php file with the path encoded as a GET variable. Here are some examples of what I mean:
http://my-site.com/this/is/the/path/index.php would become:
http://my-site.com/?path=%2Fthis%2Fis%2Fthe%2Fpath%2Findex.php
http://my-site.com/this/is/the/path/ would become:
http://my-site.com/?path=%2Fthis%2Fis%2Fthe%2Fpath%2F
http://my-site.com/this/is/the/path/index.php?v=var would become:
http://my-site.com/?path=%2Fthis%2Fis%2Fthe%2Fpath%2Findex.php%3Fv%3Dvar
How can this be done with the .htaccess file?
EDIT
To be clear, here is what I am hoping to achieve:
If a path is to a file called "index.php" it redirects as long is it is not the root index file. Note, said index.php file could have GET variables that should be included.
If a path ends in a slash, in should redirect as well as long is it is not the root. Again, note said path could have GET variables that should be included.
Otherwise, it should not redirect any other paths.
In summary, only paths to an index.php file (that are not the root) or that end in a slash should be redirected.
put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?path=$1 [L,QSA]
You can use this .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?path=$1 [L]
</IfModule>
I suggest learning a little about regular expressions in Apache's mod_rewrite.
RewriteEngine On
RewriteRule ^(.*/index.php)$ index.php?path=$1
Note this will only rewrite URL's that end in index.php and leave all other URL's alone, as that's the behavior you specified. If you want to rewrite all URL's to the index:
RewriteEngine On
RewriteRule ^(.*)$ index.php?path=$1

.htaccess rewriting url to page or directory

For my site I have a RewriteRule that points the URL http://www.mysite.com/work to a work.php file. I also have a directory called "work" that has files in it, like project1.php, project2.php, etc...
What rules would I have to write so that the URL http://www.mysite.com/work knows to go to the work.php file, but the URL http://www.mysite.com/work/project1 knows I mean to go inside the directory "work" and display the project1.php file?
EDIT: Should point out, this is what I'm currently working with:
RewriteEngine On
RewriteBase /beta/
RewriteRule ^([a-z]+)$ $1.php [L]
Any additional tips to improve this security-wise? (Stopping directory jumping, etc...)
Try this:
RewriteEngin On
RewriteBase /
RewriteRule ^work$ /work.php [QSA,L]
That will ensure that http://www.mysite.com/work (no trailing slash) will go to your work.php file.
If you also want http://www.mysite.com/work/ (with trailing slash) to go work.php, add this line just above the last RewriteRule.
RewriteRule ^work/$ /work [R=301,QSA,L]
That will redirect it to the URL with no trailing slash thus, displaying the work.php file.
UPDATE: Since you already have a RewriteBase directive, just put the RewriteRule line(s) right after your RewriteBase but before your RewriteRule as the rule you're using a catch-all and will match everything.
This is the answer to your question. It works for me.
RewriteEngine On
RewriteBase /
RewriteRule ^([a-z]+)$ $1.php [L]
Simply remove beta/
Try this rule:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .* $0.php [L]

Categories