I'm using a PHP router (AltoRouter) that allows me to define routes in all sorts of manners but doesn't do any kind of redirecting.
I want my '/' route to remain as is.
But if someone goes to anything other than '/' I have a wildcard route that grabs the name of that route, locates a twig file, and renders it based on name. So '/about' would locate about.twig and render it.
What I want to achieve is if someone goes to '/about' or '/contact', etc, then they'd be re-routed to '/pages/about', '/pages/contact', etc, but if they go to '/pages/about' directly then no re-route necessary.
My current .htaccess file looks like this:
RewriteEngine on
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
Unfortunately I did a bunch of searching around and didn't really find how to do that specific conditional re-route/redirect. Especially not one that left the root '/' alone.
P.s. pages isn't actually a directory on the server. I just want to insert 'pages' before the page name.
Try adding this immediately after RewriteEngine on:
RewriteCond %{REQUEST_URI} !=/
RewriteCond %{REQUEST_URI} !^/pages/
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule (.*) /pages/$1 [R,L]
Related
I'm working on an MVC project and I have the following .htaccess file:
Options -Indexes
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?path=$1 [L]
RewriteRule !^(public/|index\.php) [NC,F]
It works OK. I only want the public/ folder and the index.php file to be accessible to the public. All other paths should be inserted into the path GET parameter. For example, mysite.com/controller/method should point to mysite.com/index.php?path=controller/method.
Now, there is a problem. When visiting the URL directly (without including index.php, it is adding [NC,F] to the GET path parameter. It's like visiting mysite.com is pointing to mysite.com/index.php?path=[NC,F].
Why is this happening and how do I fix it?
EDIT
I moved index.php into the public/ folder. Here is my .htaccess file now:
Options -Indexes
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/index.php?path=$1 [L]
RewriteRule ^(/)?$ public/index.php [L]
RewriteRule !^(public/) [NC,F]
It seems to work OK. Are there any other improvements I could make on this?
You don't have a redirect location on the last rule, so it's taking the flags as the redirect location. Just a dash will be fine since it's a forbidden response. Change the last line to:
RewriteRule !^(public/|index\.php$) - [NC,F]
Also adding the dollar sign after index.php just to be clear.
Edit:
I would suggest updating your new rule set to the following (actually I suggest a complete re-think below, but this is an update on what you have):
RewriteEngine On
RewriteRule ^$ public/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ public/index.php?path=$1 [L]
RewriteRule !^(public/) - [NC,F]
The (/)? wasn't needed in your homepage rule, as the opening forward slash is not included in .htaccess matches anyway.
I moved your rule for the homepage to the top or it will never be used due to being matched by the previous rule (so the path param is not there when empty, which is presumably what you intended).
I stopped anything in /public/ from being passed to your index.php script, since the way you had it, anything in public that didn't exist would have been passed to your index script, which does not seem to be what you intend.
I added RewriteCond %{REQUEST_URI} !=/public/index.php so the rule couldn't be executed on itself and create a loop if rule processing is run through more than once, which it can be, but then took it back out because the above match on /public/ covers that anyway.
A Re-Think
All that said, I don't think it really makes sense to check if files don't exist and then just send forbidden responses to the ones that do, yet send everything else to your index script. Why not just send everything to your index script? That seems to be what you want really. I would suggest you simplify to this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ public/index.php?path=$1 [L]
Dropping the homepage rule since no need to worry about an empty path parameter being passed to your index script. Changing the logic to be "Leave anything in /public/ alone. For anything else, pass it through to the index.php script." so files tests not needed since the script handles it all, and no forbidden response needed because there is nothing left to match, it's all covered by the rules. You can always return forbidden to anything you don't want to process in your script, which you would have needed to do anyway for existing file URLs in your previous setup.
One Last Re-Think
And finally, if I might suggest, it would be cleaner to have your index.php file in the root of the website, so you can make /public/ work with its own index file later if you like, so finally I would move it back to the root and change the rules to this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{REQUEST_URI} !=/index.php
RewriteRule ^(.*)$ index.php?path=$1 [L]
And if you like all that, an up-vote to go with already accepting the answer would be much appreciated. :)
Adding RewriteRule ^(/)?$ public/index.php [L] seems to have resolved the issue. I'm not sure if this is the best approach, but here is my .htaccess file now:
Options -Indexes
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/index.php?path=$1 [L]
RewriteRule ^(/)?$ public/index.php [L]
RewriteRule !^(public/) [NC,F]
I moved index.php into the public folder to make things clearer.
I have got a lot of subdomains configured using wildcard e.g.
subdomain1.domain.com
subdomain2.domain.com
subdomain3.domain.com
(...)
subdomain89.domain.com
etc etc.
They are point to /public_html/. Inside the public_html I have created
/public_html/subdomain1
/public_html/subdomain2
/public_html/subdomain3
(..)
/public_html/subdomain89
sub-folders.
I would like to redirect all request from subdomains (any) to index.php files within the respective sub-folders e.g.:
http://subdomain1.domain.com/
http://subdomain1.domain.com/about_us.php
http://subdomain1.domain.com/contact.php
redirects to /public_html/subdomain1/index.php.
http://subdomain2.domain.com/
http://subdomain2.domain.com/about_us.php
http://subdomain2.domain.com/contact.php
redirects to /public_html/subdomain2/index.php etc etc.
This is my .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.domain\.com$ [NC]
RewriteRule !^([a-z0-9-]+)($|/) /%2%{REQUEST_URI}/index.php [PT,L]
When I access subdomain1.domain.com i see the index.php file from /public_html/subdomain1 but when I access subdomain1.domain.com/about_us.php i got 404. Any ideas?
Thanks
I have figured it out. This is the working code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.domain\.com$ [NC]
RewriteRule !^([a-z0-9-]+)($|/) /%2%{REQUEST_URI}/ [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
:-)
First make sure that your .htaccess file is in your document root (the same place as index.php) or it'll only affect the sub-folder it's in (and any sub-folders within that - recursively).
Next make a slight change to your rule so it looks something like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]
At the moment you're just matching on . which is one instance of any character, you need at least .* to match any number of instances of any character.
The $_GET['path'] variable will contain the fake directory structure, so /mvc/module/test for instance, which you can then use in index.php to determine the Controller and actions you want to perform.
If you want the whole shebang installed in a sub-directory, such as /mvc/ or /framework/ the least complicated way to do it is to change the rewrite rule slightly to take that into account.
RewriteRule ^(.*)$ /mvc/index.php?path=$1 [NC,L,QSA]
And ensure that your index.php is in that folder whilst the .htaccess file is in the document root.
Alternative to $_GET['path'] (updated Feb '18 and Jan '19)
It's not actually necessary (nor even common now) to set the path as a $_GET variable, many frameworks will rely on $_SERVER['REQUEST_URI'] to retrieve the same information - normally to determine which Controller to use - but the principle is exactly the same.
This does simplify the RewriteRule slightly as you don't need to create the path parameter (which means the OP's original RewriteRule will now work):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.php [L,QSA]
However, the rule about installing in a sub-directory still applies, e.g.
RewriteRule ^.*$ /mvc/index.php [L,QSA]
I've been searching for 4 hours by now and I still can't get this to work.
I have the following directories in my webroot:
- application
- assets
- css
- config
- protected
- .htaccess
- ...
- .htaccess
- framework
- [Yii framework inside]
- .htaccess
The .htaccess in my webroot should redirect/'rewrite' all requests for whatever xxx/xxx to http://www.example.com/application/ as long as the requested file or directory does not exist.
This way requests for .css, .js and other files can still work.
In my config I made sure Yii expects SEO friendly URL's and I don't tend to use the index.php. So it now looks like this:
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'' => 'site/index',
'<controller:\w+>/<id:\d+>'=>'<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
'<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
),
),
'request' => array(
'baseUrl' => 'http://www.example.com',
),
The 'request' was added later on because Yii was generating links as example.com/application/site/login. With the request Yii generates example.com/site/login instead.
In the .htaccess in my webroot I tried about everything.
First I was able to 'remove' the subdir from the URL. The index page was shown.
I tried to add a rule so all none existing directories would be redirected to the same url.
My first rule broke, and 'application' was in the URL again, but no css styles were loaded.
At this moment I got the index page with css, but now everything brings me to the index page.
my .htaccess looks like this:
RewriteEngine on
RewriteCond $1 !^application/
# if a directory or a file exists, use it directly
RewriteCond %{DOCUMENT_ROOT}/application/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/application/$1 -d
RewriteRule ^(.*)$ application/$1 [L]
# otherwise forward it to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^.*$ application/index.php
Mod_rewrite is enabled (I know because some things worked before). I looked at examples from the Yii docs.
I tried solutions from other questions on Stack Overflow like this one and many many others.
Still no luck.
Could someone please help me out?
edit:
With the .htaccess above a request to example.com ends at example.com/application .. I however would like to make the 'application' 'invisible' again (worked before, don't know why it broke)
I did change my .htaccess as follows:
RewriteEngine on
RewriteCond $1 !^application/
# if a directory or a file exists, use it directly
RewriteCond %{DOCUMENT_ROOT}/application/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/application/$1 -d
RewriteRule ^(.*)$ application/$1 [L]
# otherwise forward it to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /application/
RewriteCond %{HTTP_HOST} ^(www.)?brainfeeder.be$
RewriteRule ^$ application/$1 [L,QSA]
But still a url like www.brainfeeder.be/site/login brings me to the default controller/action which is the site/index.
I guess my conditions or rules are not exactly correct yet.
Please see my small test application I set up to tackle this issue.
What happens: brainfeeder.be get rewritten to brainfeeder.be/application/ My Yii app is in there so it runs the 'bootstrap' index.php file and gets to the default controller/action, in this case site/index.
Now when you click the 'login' button it should show you a login form. But it stays at the site/index view.
Ok, once again I updated my .htaccess a couple of times. Now I have the following situation:
www.example.com AND example.com are rewritten to www.example.com/application AND example.com/application.
(www.)?example.com/existingfolder just shows content of 'existingfolder'.
(www.)?example.com/var1/var2/../varn get redirected to (www.)?example.com/application/var1/var2/.../varn
Now the only thing I would like to happen is that the latter gets rewritten instead of redirected. So visitors don't know they are in the directory 'application'.
So (www.)?example.com/var1/var2/.../varn would bring the visitor directly to the correct page.
The contents of my .htaccess at the moment:
Options +FollowSymLinks
#IndexIgnore */*
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?brainfeeder.be$
RewriteRule ^$ /application/ [L]
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
# The directory or file does not exist
RewriteCond %{REQUEST_FILENAME} !-s [OR]
RewriteCond %{REQUEST_FILENAME} !-l [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /application/$1 [L,R]
The thing is, when losing the R flag in the last RewriteRule will bring me to the index.php file inside 'application' but it shows the home page instead of a login form when for example I go to example.com/site/login.
Which, I guess, the script does not see the vars. (if it did site/error would trigger) So it handles this as a request to example.com/application and not as example.com/application/var1/var2
I hope I did explain the problem better this time.
Thanks for the great help 'till now.
Try to check these configuration directives if you just want to rewrite all the unexisting /$var1/$var2 to /application/:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /application/
Now, if you want those unexisting files to redirect, rather than to rewrite them? Then just add a [R] flag at the end of the RewriteRule directive, just don't forget a single " " space before the flag.
Now, if you want to redirect /application to / and then rewrite /index.php to /application and to rewrite also the unexisting /$var1/$var2 to /application/$var1/$var2 then it's quite hard (and need some exact details) but you could still try these configuration directives:
RewriteEngine on
# rewrite index.php
RewriteRule ^index.php$ /application
# rewrite unexisting files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /application/$1/$2
# try to remove this if it causes redirection loop
RewriteRule ^application/?$ / [R]
You can also try to use DirectoryIndex application/index.php at the very top of those directives to change the index of your site and remove the line RewriteRule ^index.php$ /application if it causes an error.
Actually, I can't understand your question.. You said:
The .htaccess in my webroot should redirect/'rewrite' all requests for
whatever xxx/xxx to http://www.example.com/application/ as long as the
requested file or directory does not exist. This way requests for
.css, .js and other files can still work.
And now, you said to your comment:
So any link to brainfeeder.be/application/$var1/$var2 should be shown
as brainfeeder.be/$var1/$var2
If you would also like to redirect existing /application/$var1/$var2 to /$var1/$var2 then please try to add these directives, and if it causes an error to your system, just remove it:
# the condition is important as mentioned above
RewriteCond %{REQUEST_URI} !\.css$
RewriteRule ^application/([^/]+)/([^/]+)/?$ /$1/$2 [R]
You can add another condition (as many as you like) at the top of the RewriteRule, just use your thinking if you're a programmer. You could add another condition like RewriteCond %{REQUEST_URI} !\.jpg$ if you doesn't want to redirect the file with an extension like .jpg or else that you want such:
RewriteCond %{REQUEST_URI} !\.css$
RewriteCond %{REQUEST_URI} !\.jpg$
RewriteCond %{REQUEST_URI} !\.png$
RewriteRule ^application/([^/]+)/([^/]+)/?$ /$1/$2 [R]
Please try to change the source of your .htaccess file with this code:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?brainfeeder\.be$
RewriteRule ^/?$ /application/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /application/$1 [L]
Not sure what your question is ...
But here is an .htaccess that should accomplish what you want:
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
You dont need to edit .htaccess. You just need to move the Yii entry script (index.php) and the default .htaccess up from the subdirectory to the webroot (so that they reside directly under public_html). After you move index.php and .htaccess to the root directory, all web requests will be routed directly to index.php under webroot (rather than to the subdirectory), thus eliminating the /subdirectory part of the url.
After moving the files, you will need to edit index.php to update the references to the yii.php file (under the Yii framework directory) as well as the Yii config file (main.php). Lastly, you will need to move the assets directory to directly the webroot, since by default, Yii expects the assets directory to be located in the same location as the entry script).
That should be all you need to do, but if you need more details I describe the approach more fully here:
http://muhammadatt.tumblr.com/post/83149364519/modifying-a-yii-application-to-run-from-a-subdirectory
Desired goal:
On an existing website, I need to implement a new website. The new site needs to be 'staged' implementation, so a subdirectory for 'dev' and for 'test' and for 'site' is made. All three subdirectories contain an identical php/mvc implementation except for the fact that different useraccounts and databases are used. The current site which resides in the rootdirectory should be kept working as well.
Current configuration attempt:
I have put an .htaccess file in the root directory of the current website.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/dev(.*)$ <url>/dev/$1
RewriteRule ^/test(.*)$ <url>/test/$1
RewriteRule ^/site(.*)$ <url>/site/$1
Note: instead of there is a complete url.
This should redirect all urls with 'dev' in it, to /dev/$1 and $1 contains all the rest of the parameters.
Result:
When the homepage of 'dev' of 'test' or 'site' is called. The homepage shows up perfectly fine.
Problem:
Everytime I click on a menu-item or a link, the resulting url gets rewritten to the right url, which is: /dev/volunteering?page=volunteering&lang=nl
but I get an errormessage 404:
The requested URL /dev/volunteering was not found on this server.
Please keep in mind that it is about a custom mvc implementation.
Required Help:
What change is necessary to make the browser understand that this kind of requests should not be handled by the current website but that, depending of 'dev' or 'test' or 'site', the request should be forwarded to the appropriate subdirectory in such way that the mvc implementation understands?
Please try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(dev)(volunteering([^?]*)) $1/$2?page=$2&lang=nl [L,NC]
RewriteRule ^(test)(volunteering([^?]*)) $1/$2?page=$2&lang=nl [L,NC]
RewriteRule ^(site)(volunteering([^?]*)) $1/$2?page=$2&lang=nl [L,NC]
Every single time a user registers on my site I would like them to have their own subdirectory with their registered "username". Every user subdirectory will have the same "index.php" file which will do something.
For example: "/users/username1/" and "/users/username2/"
If some one wants to access the subdirectory they would simple go to:
"www.example.com/users/username1/" or "www.example.com/users/username2/"
The easy and messy solution would be to simply create a subdirectory for every user and place the same "index.php" file in every directory. But to me this is only going to crowd my server space and make my directories large.
I wanted to know if all this can be done using .htaccess? Can I create one "index.php" and one ".htaccess" file and place them both in my "/users/" directory? What would be the actual code that I would have to place in my .htaccess file??
If you have a better way of doing this please let me know. I am using Apache and PHP as my working environment.
Thank you
Well, for example, you could do it all with one htaccess like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
What it does:
switches on rewrite engine
checks if a requested file exists
checks if a requested directory exists
if NOT, it redirects request to your main index.php
Basically that means if you enter url such as yourdomain.com/users/ivan/, you request will be redirected to:
index.php?url=/users/ivan
then you $_GET['url'] in your index.php and split it into pieces.
That's just an example, there other mod_rewrite methods to do this.
Make it virtual. There are no subdirectories, you can use mod_rewrite to simulate that.
With mod_rewrite you can make /users/username1 lead to /users.php?user=username1 for instance. Everything is transparent for the client, he wont notice what is really happening.
By using something like this:
RewriteEngine On
RewriteRule ^([\-_0-9A-Za-z]+)$ index.php?a=$1 [L]
You can customize RewriteRule as much as you want.
You can essentially type in any directory you want, and it will be redirected to your index.php page.
If you want to make sure the existing directories are not redirected, do this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\-_0-9A-Za-z]+)$ index.php?a=$1 [L]
If you want to limit the scope, so only a subdirectory of user/ is redirected (similar to Stack Overflow), simply add in 'user' to the start of the rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^user/([\-_0-9A-Za-z]+)$ index.php?a=$1 [L]
And finally, if you want to have an individual file handle all user requests seperate from your actual index.php page:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^user/([\-_0-9A-Za-z]+)$ users.php?a=$1 [L]
This is a very similar setup I use to distribute CSS files.
Note: The Directory will be contained is $_GET['a']