.htaccess not working for php/mvc in subdirectory - php

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]

Related

Why the url in which index.php is prefix with first segment in codeigniter works | Codeigniter | RewriteCond

I am working on a CodeIgniter project and today I found a very strange issue.
When I open the URL that is prefixed with index.php in the first segment it is still working even though I expect the URL to return a 404 Not Found page.
For example, the URL of my website is http://localhost/project and when I open the URL http://localhost/project/jobs it works fine, but when I open http://localhost/project/index.phpjobs it also works.
I don't know what is going on over here!
Please note that the URL doesn't include slash but is still working and that is not a typo.
Please check in your project and let me know if someone have the same problem because I think this problem may also exist in your current project but not noticed.
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin(.*)$ admin/index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteRule ^.well-known/ - [L,NC]
Your first rewrite rule
RewriteRule ^admin(.*)$ admin/index.php?/$1 [L]
will be honored only if the previous two conditions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
have been met.
Now, the trick lies in these conditions which basically say that the rewrite will be performed only if the requested resource (REQUEST_FILENAME here) does not exist as either a file or a folder.
Since index.php obviously exists the rewrite rule is skipped and the server actually receives the original (non-rewritten) request.
That is the reason why you see the same result for requests that both do and do not contain /index.php/ as prefix.
The same applies for both sets of rewrite, the one you are using for your admin page and the regular one.

Redirect a page from one domain to another domain (both domains belong to one website)

At first glanse question is pretty common, however other answers I have seen do not work in my case.
I need to redirect a page from one domain to another domain. Both domains have one web-site under them and one common .htaccess file.
www.olddomain.com/guides -> www.newdomain.com
I use .htaccess file. If someone knows answer for other way - you are welcome also.
My current file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([a-z_\d]+)$ controller.php?page=$1 [QSA]
I have tried this way:
RewriteEngine on
Redirect 301 http://www.olddomain.com/guides https://www.newdomain.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([a-z_\d]+)$ controller.php?page=$1 [QSA]
Also I tried this way:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^http://www.olddomain.com/guides$ https://www.newdomain.com [R=301]
RewriteRule ([a-z_\d]+)$ controller.php?page=$1 [QSA]
Does not work. How it can be done properly?
I believe your Redirect 301 line is the way to go, but should read something like this:
Redirect 301 /guides https://www.newdomain.com
See here for more information.
Edit to add:
The above will redirect to https://www.newdomain.com/
If you want a certain path, then just put
Redirect 301 /guides https://www.newdomain.com/foo
where foo is the path on the server you want to end up at.
I end up with the following.
Unlinked "olddomain" name from the origin site.
Created new website folder at my hosting with only one file .htaccess in a root that contains only one line as #CBaish suggested:
Redirect 301 /guides https://www.newdomain.com
Linked "olddomain" to this folder
Now all traffic from the page "/guides" of olddomain will go to my newdomain.

.htaccess redirect to /pages unless index

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]

Why can some urls contain "fake" directories?

Currently, I'm trying to set up updates for passes that are added to the Wallet app on iOS.
One thing that is interesting is that having the url https://example.com/index.php/var1/var2 still works and index.php is still run. Is there a reason why this url format works?
.htaccess/mod_rewrite is the reason why it's working.
For example:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]
Having this rules inside a file named .htaccess in the root folder of your website, will make the path /var1/var2 available inside $_GET['path']

Create dynamic subdirectories using htaccess and php

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']

Categories