What's wrong with this mod_rewrite RewriteRule code? - .htaccess

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
I'm using wamp apache on windows 7. mod_rewrite.so has been enabled in httpd.conf. This code was directly taken from a tutorial found here: http://anantgarg.com/2009/03/13/write-your-own-php-mvc-framework-part-1/
It's purpose is to redirect calls to the ./public folder where the actual logic lies.
I get an internal server error (http 500) when I try to browse index.php in the directory of this .htaccess file. When I remove the two rewriterule statements, it works.
Not sure why, but no error shows up in the logs folder. There is no error log whatsoever.
Any clue on what's going wrong here?

Your expression, (.*), matches the target, /public/something. The rewrite engine loops until the request URI stops getting changed, and your request URI is turning into /public/public/public/public/public etc because there's nothing that stops the rewriting process. Try adding a condition to prevent the loop:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule (.*) public/$1 [L]
</IfModule>
So if the request already starts with /public, don't apply the rule.

Related

Simple rewritecond in htaccess doesn't work as expected

This is super simple but it's driving me crazy! I have a website at http://example.org/ and a subdirectory at http://example.org/ccc/
I want to redirect anything outside of the /ccc/ directory to a different website.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/ccc/?.*
RewriteRule ^(.*)$ https://new-website.com/$1 [L]
But this code doesn't work, it redirects the /ccc/ directory. According to my research and testing with this htaccess tester, it should not redirect because the RewriteCond is checking against /ccc with optional slash and other characters after it.
What is happening? Does this look correct?
Edit: This method from this answer is also not working, the CCC domain is being redirected:
RewriteEngine on
RewriteRule ^ccc index.php [L]
RewriteRule (.*) https://new-website.com/$1 [R=301,L]
PHP 5.4.45, Apache/2.2.31
Assuming ccc/ directory doesn't have a separate .htaccess, you may use this rule:
RewriteEngine on
RewriteCond %{THE_REQUEST} !\s/ccc[/?\s] [NC]
RewriteRule ^ https://new-website.com%{REQUEST_URI} [L,R=301,NE]
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of other rewrite directives. An example value of this variable is GET /index.php?id=123 HTTP/1.1
It looks like [L] isn't behaving normally and I'm guessing it's the old version of Apache (2.2.31) because these rules worked on a separate website. I found this solution which seemed to work for this case, the third line below:
RewriteEngine on
RewriteRule ^ccc/? index.php [L]
RewriteCond %{ENV:REDIRECT_STATUS} != 200
RewriteRule ^(.*)$ https://new-website.com/$1 [L]
Explanation from that question:
The problem is that once the [L] flag is processed, all the next RewriteRules are indeed ignored, however, the file gets processed AGAIN from the begin, now with the new url.
This magic Condition will not process the catch all if the file was already redirected.

Redirect url with .htaccess

I m sure that many people will say that this is duplicated but I try everything from other "Question"`s and nothings work for me.
The problem is that I move my project to the web server.
In this server I have folder "public_html" where I but my project Symfony.
Now to enter on my project I must write the following url: www.mydomain.com/Symfony/web/*
But I want to write a Rewrite Rule which will redirect from www.mydomain.com/Symfony/web/* to
www.mydomain.com/home/*.
To do this I try on 2 different ways with many combination of ReWrite rule.
In my public_html I create a .htaccess folder
I edit the .htaccess in Symfony/web folder
I add the following rule in both file but without success
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^Symfony/web/(.*)$ www.mydomain.com/home/$1 [L,R=301]
Unfortunately without success. What I`m doing wrong?
My htaccess file look like
And all the time Error 404 Object not found
Symfony/web/.htaccess
DirectoryIndex app.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Symfony/web/
RewriteCond %{THE_REQUEST} \s/+Symfony/web/ [NC]
RewriteRule ^(.*)$ /home/$1 [L,R=301]
RewriteRule ^home/(.*)$ $1 [L,NC]
</IfModule>
It`s redirecting me but I receive again Object not found :(
I delete the .htaccess in public_html folder which is the root one for my server
public_html\.htaccess
RewriteEngine On
RewriteRule ^home/(.*)$ /Symfony/web/$1 [L,NC]
1: Place this code in /Symfony/web/.htaccess:
RewriteEngine On
RewriteBase /Symfony/web/
RewriteCond %{THE_REQUEST} \s/+Symfony/web/ [NC]
RewriteRule ^(.*)$ /home/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [L]
2: Place this code in /public_html/.htaccess:
RewriteRule ^home/(.*)$ /Symfony/web/$1 [L,NC]
I'm going to go out on a limb and say that your rule is backwards. I think you want your URL to be www.mydomain.com/home/* in the browser... In which case the rule would be reversed. Also, your .htaccess should be in the root and you don't need to include the domain in the rewrite rule because you set a rewrite base.
RewriteRule ^home/(.*)$ Symfony/web/$1 [L,R=301]

Symfony: htaccess to hide app.php or app_dev.php

I know this has been asked many times, but something strange is happening to me. Apache DocumentRoot is pointing to symfony/web/ and this is my .htaccess inside web/ dir:
DirectoryIndex app_dev.php
#DirectoryIndex app.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{CONTEXT_PREFIX}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ app_dev.php [QSA,L]
#RewriteRule ^(.*)$ app.php [QSA,L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule .? %{ENV:BASE}app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /app.php/
</IfModule>
</IfModule>
Well, the thing is www.example.com/route1/ is working and www.example.com/route2/ is throwing an error:
Oops! An Error Occurred
The server returned a "404 Not Found".
Something is broken. Please e-mail us at [email] and let us know what you were doing when this error occurred. We will fix it as soon as possible. Sorry for any inconvenience caused.
While www.example.com/app_dev.php/route2/ is working ok (also www.example.com/app_dev.php/route1/)
Help?
Update. Cache clear in prod throws this error (I never tried before, I'm working on dev):
[Doctrine\Common\Proxy\Exception\UnexpectedValueException]
The type hint of parameter "userRoles" in method "addRole" in class "MyProject\PanelBundle\Entity\User" is invalid.
[ReflectionException]
Class Maycol\BlogBundle\Entity\Role does not exist
This is the .htaccess that worked for me:
DirectoryIndex app.php
#DirectoryIndex app_dev.php
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect to URI without front controller to prevent duplicate content
# (with and without `/app.php`). Only do this redirect on the initial
# rewrite by Apache and not on subsequent cycles. Otherwise we would get an
# endless redirect loop (request -> rewrite to front controller ->
# redirect -> request -> ...).
# So in case you get a "too many redirects" error or you always get redirected
# to the startpage because your Apache does not expose the REDIRECT_STATUS
# environment variable, you have 2 choices:
# - disable this feature by commenting the following 2 lines or
# - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
# following RewriteCond (best solution)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
#RewriteRule ^app_dev\.php(/(.*)|$) %{CONTEXT_PREFIX}/$2 [R=301,L]
RewriteRule ^app\.php(/(.*)|$) %{CONTEXT_PREFIX}/$2 [R=301,L]
# If the requested filename exists, simply serve it.
# We only want to let Apache serve files and not directories.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteCond %{REQUEST_FILENAME} -f
#RewriteRule ^(.*)$ app_dev.php [QSA,L]
RewriteRule ^(.*)$ app.php [QSA,L]
# The following rewrites all other queries to the front controller. The
# condition ensures that if you are using Apache aliases to do mass virtual
# hosting, the base path will be prepended to allow proper resolution of the
# app.php file; it will work in non-aliased environments as well, providing
# a safe, one-size fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
#RewriteRule .? %{ENV:BASE}app_dev.php [L]
RewriteRule .? %{ENV:BASE}app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
# When mod_rewrite is not available, we instruct a temporary redirect of
# the startpage to the front controller explicitly so that the website
# and the generated links can still be used.
#RedirectMatch 302 ^/$ /app_dev.php/
RedirectMatch 302 ^/$ /app.php/
# RedirectTemp cannot be used instead
</IfModule>
</IfModule>
This error is NOT Apache/htaccess related. This 404 error page is the default one of symfony itself!
Something is broken. Please e-mail us at [email] and let us know what you were doing when this error occurred. We will fix it as soon as possible. Sorry for any inconvenience caused.
There is no route matching /route2. Check your routing like this:
php app/console router:debug
To inspect it quicker use grep or findstr ( Windows )
php app/console router:debug | grep route2
Please also check that your route is not only configured in the dev environment ( i.e. only in routing_dev.yml )!
php app/console router:debug --env=prod
please clear your prodution cache with ...
php app/console cache:clear --env=prod
... and check your log files if there is an uncaught Exception leading to the 404 page displayed when accessing the page. You can for example use this command to view the live changes in the production logfile.
tail -f app/logs/prod.log

Exclude a root folder from CakePHP app

I am trying to exclude a first level directory from Cake (root folder app), as it should hold a different app.
/ holds the app
/development/ holds the tested version of the app
The directory structure:
Public folder
.htaccess [modified]
app
.htaccess [unmodified]
webroot
.htaccess [unmodified]
index.php [unmodified]
...
...
lib
Cake
...
development
app
webroot
index.php [dumps $_SERVER for test purposes]
So, my development structure still doesn't have an app inside (nor .htaccesses), just to test if my root .htaccess works.
This is my modification of the root .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/development.*
RewriteRule (.*) - [L]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
What happens:
/development/ shows the apache index of development folder
/development/app/ shows the apache index of app folder
/development/app/webroot shows the root app (request is captured in spite of the development url match).
/development/app/webroot SHOULD show me my /development/app/webroot/index.php file, right?
What the hell am I missing here?
This turned out to be an oddball bug on my server.
The resulting .htaccess which works now is:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/development(?:$|/)
RewriteRule ^$ app/webroot/ [L]
RewriteCond %{REQUEST_URI} !^/development(?:$|/)
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
For reasons unknown to me, if I SSH'd to my user account and edited the .htaccess files through the command line, it wouldn't work!
When I uploaded the same files (via SFTP), it started working properly.
I am using cPanel (11.34.1) on CentOS (5.9).
Another example: if you wish to "ignore" multiple sub-folders on a CakePHP installation, you can use this pattern:
<IfModule mod_rewrite.c>
RewriteEngine on
# /development, /version1.5 and /version1.8 directories
# they all hold independent apps
# or completely non-cake-related content
RewriteCond %{REQUEST_URI} !^/(?:development|version1\.5|version1\.8)(?:$|/)
RewriteRule ^$ app/webroot/ [L]
RewriteCond %{REQUEST_URI} !^/(?:development|version1\.5|version1\.8)(?:$|/.*)
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
Note that both RewriteCond instructions are identical.

CakePHP HTAccess 500 Internal Server Error, 404 Page Not Found errors

I'm using CakePHP 1.2 Version and When I upload my site on Live Server, it gives me 500 Internal Server Error
When I check mod_rewrite in phpinfo function, its not visible, but on same server another Joomla site is running perfectly fine without any problem of htaccess or path rewrite issue with AceSEF Component of Joomla for htaccess page / paths, so I believe as this is Shared server it must be that way.
I have tried different options like adding 'RewriteBase /' in root .htaccess file or trying the same in app/webroot/.htaccess files as well but no solution works for me.
Even when I write, 'RewriteBase /' in root .htaccess and app/.htaccess file, error changes to 400 Page Not Found. I don't know why.
Below is the .htaccess code:
root .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
app/.htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
app/webroot/.htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]
</IfModule>
I have read through some links like: Apache-and-mod_rewrite-and-htaccess though not much helpful
Any answer or solution for this error? Much appreciated !!
Thanks
Check if the rewrite module is linked in the enabled modules of apache. Like you said it isnt listed in phpinfo() so the module maybe isnt enabled and joomla has some workarounds.
(use following commands at your own risk, you need root access)
under debian/ubuntu; apache2 enable the module like that
cd /etc/apache2/mods-enabled/
ln -s ../mods-available/rewrite.load rewrite.load
you have to restart apache2
service apache2 restart
Then the module should be enabled! So try again.
Alternatively, tell the url you try to open!

Resources