apache2 silent redirection not working as expected - mod-rewrite

I have a web server using apache2, and I want to silently redirect some pages.
So if an user try to access "http://website.com/test.php", the page at "http://website.com/fool.php" will be used instead. And i need to do that for several pages, not just test.php.
I though I could use the mod_rewrite module for this purpose, but I cant get it working.
I tried adding in apache2.conf, so i could have at least just a redirection for now (not silent):
RewriteEngine on
RewriteRule "^/test.php$" "/fool.php"
or
<Directory "/var/www">
RewriteEngine on
RewriteBase "/var/www"
RewriteRule "^/test.php$" "/fool.php"
</Directory>
This result in the test.php not being redirected at all (it still can be accessed), but also, it breaks my custom 404 and 403 pages with the error "ForbiddenYou don't have permission to access this resource.Additionally, a 403 Forbidden error was encountered while trying to use an ErrorDocument to handle the request.".
Apache2 is not complaining about the syntax when i restart it.
Setting it in my sites-enabled config leads to the same result.
Note that the goal is not to prevent the direct access to test.php, and the request must result in a 200 success code.
There may be something i do not understand, because searching on the net, it looks like it's the way to achieve this.

You may try an alternative method: use the .htaccess file. (place the file in the root directory of your site)
For example:
RewriteEngine on
RewriteRule ^/test.php$ /fool.php [L]

Related

Transferring codeigniter HMVC project from localhost to live server

I made a web application on codeigniter HMVC. I tested it on local server and the following link works perfectly fine by giving me a login form, the link : http://localhost/bird_vision/users/login, where my directory structure goes like this bird_vision/application/modules/users/users.php (users->module's name, users.php->controller's name). Now I posted the whole "bird_vision" folder to web using filezilla.
- changed the base_url in config.php file to domain_name
- Made some changes in .htaccess file
# Customized error messages.
ErrorDocument 404 /index.php
# Set the default handler.
DirectoryIndex index.php
# Various rewrite rules.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$10 [L,QSA]
</IfModule>
Now when I write bird_vision.se/bird_vision/users/login in my address bar it gives me 404 page not found. Although when I when type birdvision.se/bird_vision/welcome it gives me welcome to codeigniter page.
Please I need help as my all hardwork making this project is at stake. Thanks in advance.
I figured the problem was not with HMVC but was with codeigniter version 3.0. When it comes to unix(which was the operating system running on my server), it is case sensitive. So when I tried to access the controller it could'nt find the file because of case sensitivity and a 404 popped out everytime.
My folder structure before was something like :
-application
-->modules
--->users
---->controllers
----->users.php
And the link on which I was getting 404 was www.example.com/users/login
where "users" in the link is controller's name within the "controllers" folder under "modules" folder in my case named as users.php and "login" is the function inside the controller.
All I had to do was to change the case of the first letter of my module and controller name. Now my directory looks like
-application
-->modules
--->Users
---->controllers
----->Users.php
note : capitalized "U" in Users module and Users controller.
And the the url I used to access the users controller was : www.example.com/Users/login
And I was back in business.

Add prefix to route using apache2 mod_rewrite

I'm trying to use apache2's mod_rewrite to add a prefix to all my routes in my symfony2 2.1 project.
Let's say my symfony2 project's root is in /var/www/ProjectFrontEnd.
What I want to achieve is to let the user access the project using http://www.mydomain.com/Project
I have set up this in my virtual host:
DocumentRoot var/www/ProjectFrontEnd
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/Project/(.*)$ /var/www/ProjectFrontEnd/web/app.php/$1 [QSA,L]
When I look at the rewrite log, I can see that the route is rewritten as I expect (rewrite '/Project/' -> '/var/www/ProjectFrontEnd/web/app.php/') but I get a 404 error from symfony and the log says something along the line of request.ERROR: Symfony\Component\HttpKernel\Exception\NotFoundHttpException: No route found for "GET /Project/" (uncaught exception)
I don't understand why symfony2 still uses the non-rewritten url...
I'd like to specify that prefixing my routes in symfony's routing.yml is not an option.
Is what I want possible to achieve?
In this way it's not possible, cause symfony handles every rewrite as a complete route.
You can simply use the prefix in your app/config/routing.yml. I know it's neccesary for every route import to add it, but the easiest and robust way.
acme_user:
resource: "#AcmeUserBundle/Controller/"
type: annotation
prefix: /Project

getting path error (404) in .htaccess

I am using .htaccess file for redirecting page. I have tried both paths, absolute and relative, but in both the cases I am getting error: URL not found (404).
Please guide me proper way to mention path in .htaccess
RewriteRule ^/abc/([a-zA-Z0-9]+)/?$ /abc.php?tag=$1
You may need to RewriteBase /.
Try adding that just before your RewriteRule, i.e:
RewriteEngine On
RewriteBase /
Also try removing the slash before abc:
RewriteRule ^abc/([a-zA-Z0-9]+)/?$ abc.php?tag=$1
One good way to debug the URL rewriting, is to switch on the rewrite logging.
In Apache 2.2:
RewriteLog file-path.log
RewriteLogLevel 3
The syntax is different for Apache 2.4. Read more here.

issue with shifting Kohana 2.4 from root location to make it accessible via a directory?

I have developed a kohana project 2.4 version, with i has been developed by configuring virtual host and we are accessing it using some domain aa.com, now i have to move the project to folder named kohana and make it access via aa.com/kohana. If i access it this way then my url changes, So the css & js file are not included since the src url i have provided like /public/css/ so it's not working, same goes for calling a controller. like il be calling a controller using this format /user/login/. now the url when i click to shows like aa.com/user/login which is not found, in turn it should automatically comes like aa.com/kohana/user/login. Is there any way by changing the htaccess file or hacking routes.php file. ?
I should able to automatically append /kohana/ before the url.
Please suggest me a method how it can be achieved.
Yes there is!
I've only started after kohana 3.0 but you can just change the rewritebase.
In your .htacces it probably says "rewritebase /"
Change this to your new directory. "rewritebase /kohana/"
If 2.4 has a bootstrap (i'm not sure) somewhere it should say "base_url". You change this the same as the .htaccess. /kohana/.
edit: here's an example:
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /kohana/
# Protect application and system files from being viewed
RewriteRule ^(application|modules|system) - [F,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT,L]

How to use mod_rewrite in Apache2?

I´m trying to do a simple redirection with Apache2 with mod_rewrite. I've installed the module in my Apache and I´ve set a webpage in localhost/file1/file2/page.html. I´m writing this in my 000-default file:
RewriteEngine On
RewriteRule elegantdirectory/page.html file1/file2/page.html [L,NC]
So now if I try to access to localhost/elegantdirectory/page.html, the server is supposed to show me the page.html I have in file1/file2. Any ideas why isn't it working?
Try including bounds and a leading slash in your RewriteRule:
RewriteRule ^/elegantdirectory/page.html$ /file1/file2/page.html [L,NC]

Resources