Apache2 Mod rewrite page not found - .htaccess

I'm sorry that I'm not exactly sure how to phrase the question in a single sentence.
Basically, mod rewrite is working for our traq pages (code block 1 below), but not for our wordpress pages (code block 2 below). For some reason, if we implement the automatically generated Wordpress .htaccess file, the page comes up with Apache's "page not found" error (which means that it's never hitting Wordpress's index page at all).
Traq:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Wordpress:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
<IfModule>
We tried commenting out a few of the various lines in the Wordpress file, and altering various lines as we thought one thing or another might have been causing the issue. In the end, none of our changes got anything to display except 404.

We found the issue.
Our server is set up pretty much as Ubuntu from distro, and by default the apache package is configured to allow overwrite with htaccess / mod-rewrite, but not to allow any rewrite rules. We had to add the rules we wanted to allow to the apache config.

Related

Yii2 app in a subdirectory of a Wordpress website: cannot enable pretty urls

I’m trying to deploy a basic webapp on a shared environment where Wordpress is on the root. The Yii2 app is in /subfolder.
I’m following this guide. In root’s .htaccess I added:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /subfolder
RewriteCond %{REQUEST_URI} ^/subfolder
RewriteCond %{REQUEST_URI} !^/subfolder/web
RewriteRule ^assets/(.*)$ /web/assets/$1 [L]
RewriteRule ^css/(.*)$ /web/css/$1 [L]
RewriteRule ^js/(.*)$ /web/js/$1 [L]
RewriteRule ^images/(.*)$ /web/images/$1 [L]
RewriteRule (.*) /web/$1 [L]
RewriteBase /subfolder
RewriteCond %{REQUEST_URI} ^/subfolder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /web/index.php
</IfModule>
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
But with these rules added all Wordpress’ pages are handled (or attempted) through Yii, so this breaks the blog installation. It’s the first block of rules capturing all the pages, but I don’t understand why as the two RewriteCond should intercept only the Yii app URIs. I checked mod_rewrite docs but couldn’t understand what’s wrong. Any help is appreciated. Thanks
RewriteBase /subfolder
You cannot set multiple RewriteBase directives in the same .htaccess file. The last instance "wins" and controls the entire file. So, in the .htaccess file you posted, RewriteBase / set in the WordPress code block, is what is actually set for the file.
However, none of the directives actually make use of the RewriteBase directive anyway - so none of the RewriteBase directives are actually doing anything. The RewriteBase directive only applies where you have set a relative path (not starting with a slash) in the RewriteRule substitution string.
but I don’t understand why as the two RewriteCond should intercept only the Yii app URIs.
RewriteCond %{REQUEST_URI} ^/subfolder
RewriteCond %{REQUEST_URI} !^/subfolder/web
RewriteRule ^assets/(.*)$ /web/assets/$1 [L]
Presumably it's these two RewriteCond directives you are referring to... in which case these two conditions aren't really doing anything. RewriteCond directives only apply to the first RewriteRule directive that follows, so it only applies to the directive that rewrites your assets.
However, this RewriteRule is matching /assets in the document root, not /subfolder/assets, which is presumably the requirement - so these rules will fail to match.
But with these rules added all Wordpress’ pages are handled (or attempted) through Yii, so this breaks the blog installation.
The rules will certainly "break the blog installation", however, they don't appear to get as far as handling the request "through Yii". There's nothing that actually rewrites the request to /subfolder. However, the following directive unconditionally rewrites everything to the /web directory in the document root (which presumably does not exist) - so this will certainly "break" all the WordPress URLs.
RewriteRule (.*) /web/$1 [L]
In fact, I would have expected this to have created a rewrite-loop (500 Internal Server Error response)?! Unless you have a subdirectory /web off the document root which also contains an .htaccess file containing mod_rewrite directives? But that seems unlikely, since the /web directory should be inside the /subfolder directory?
Try the following instead:
RewriteEngine On
RewriteRule ^(subfolder)/(assets|css|js|images)/(.*) $1/web/$2/$3 [L]
RewriteRule ^(subfolder)/((?!web).*) $1/web/$2 [L]
RewriteRule ^subfolder/web/index\.php - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(subfolder)/. $1/web/index.php [L]
# BEGIN WordPress
:
No need for the <IfModule> wrapper. Or the RewriteBase directive.
Alternatively
However, it would be preferable to move these directives into their own .htaccess file in the root of the project, ie. /subfolder/.htaccess - which I believe is what the linked "guide" is suggesting. This keeps the two projects entirely separate. And avoids having to explicitly include the /subfolder in the directives.
In addition, creating a another .htaccess file in the web subdirectory, ie. /subfolder/web/.htaccess. This is again, suggested in the linked "guide". However, this also negates the need for the additional directives to route the request in the parent .htaccess file.
For example, putting these changes together, the /.htaccess file in the document root should only have the WordPress directives. And then...
/subfolder/.htaccess
RewriteEngine On
RewriteRule ^((?:assets|css|js|images)/.*) web/$1 [L]
RewriteRule ^((?!web).*) web/$1 [L]
/subfolder/web/.htaccess
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Again, no need for the RewriteBase directive here - in fact, using RewriteBase here arguably complicates things. When in the /subfolder/web/.htaccess file, all relative URL-paths are relative to that directory.
So, requesting /subfolder/foo gets internally rewritten by the /subfolder/.htaccess file to /subfolder/web/foo. Which is then caught by the /subfolder/web/.htaccess file (preventing a rewrite loop) and internally rewritten to /subfolder/web/index.php (providing foo does not exist as a physical file).

Understanding my .htaccess file in order to rewrite X to X.php

I have a website of PHP files that I'm hosting on SiteGround.com and want the links to look like "example.com/about" instead of "example.com/about.php". I also want my index.php to be loaded when I visit "example.com" instead of "example.com/index"
So I did some searching and this is what I want:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
But the webserver already has a default .htaccess file which has this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule . /index.php [L]
</IfModule>
Okay...so I tried to piece together what it is doing. Based on my understanding, it enables the RewriteEngine, then sets RewriteBase to be /. It then sets a rule looking for index.php and if it finds it, it doesn't check any other rule.
Otherwise, it falls through and checks if a string is not a file and not a directory and contains a .php extension, it redirects the root to index.php and stops.
So, I tried adding my three lines to the end of block right before the closing </IfModule> tag and it didn't quite work. Can anyone help me out?
EDIT
I modified my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>
In my HTML, I have:
<a href="about" .... ></a> <!-- link to about.php -->
<a href="/" ....></a> <!-- link to index.php -->
Is that right? It doesn't seem to work for me.
First of all, the line:
RewriteCond %{REQUEST_FILENAME}\.php -f
Is not part of the original .htaccess file. It can't be, it makes no sense where it is, perhaps you added it yourself while experimenting.
The .htaccess file you have there, other than that line, is a standard method of putting everything through index.php. Probably for WordPress. So you need to decide what you want to do. Do you want to put everything through index.php, or do you want to use separate .php files with the extension added on the server side? Or do you want to do both? (in which case a combined approach needs writing) Are you still using WordPress? Perhaps you can just remove those rules.
Let me know and I can update the answer if you need more info.
Update
Your rules can be modified to:
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.php$
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L,NE]
That should fix it, I think the \. on the REQUEST_FILENAME line was breaking it, the rest are improvements.
If that's not working then perhaps mod_rewrite isn't enabled, or .htaccess files are not enabled. You can test it by putting some garbage on a line and see if you get a 500 error.
If the root doesn't serve index.php then you just need to add a DirectoryIndex.

Limit RewriteRule in htaccess file

i got a site php based with some rules in the .htaccess file to get rid of file extentions in the url adress bar. Basically it takes http://netbureau.com.br/en/about.php/ and turns it into http://netbureau.com.br/en/about.
Here are the lines in the htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
The problem comes when i try to access the rss feed of the blog at http://netbureau.com.br/blog/?feed=rss2 and when i try to set custom permalinks for the blog at http://netbureau.com.br/blog. It gets messed up by the htaccess file.
So is there any way to disallow the RewriteRule for the /blog folder so that i can get back my rss link and set custom permalinks in the blog?
I know it's at the same time Wordpress related but it feels more connected to the htaccess file than Wp.
Thanks in advance.
EDIT #1:
I've set up the wordpress permalinks to the default structure which goes like this: http://netbureau.com.br/blog/?p=123 This made my rss link back for good.
The remaining problem is that Wordpress gives me its own rewriterule which is:
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
Is there a way to to still use the first rule to apply to the whole site except the /blo/ folder and apply the WP rule only to the /blog/ folder?
I've tried different combinations but without luck so far. I could only have the site without the custom links for the blog or the custom blog links and a 404 on the pages of the site.
Try:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/blog/?
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
The !^/blog/? expression excludes any URI that starts with /blog

Expression Engine - htaccess to hide index.php is not working. Why?

I have an MSM install with four (licensed) sites. Three of them work perfectly and behave as they should. The fourth one, currently under construction seems to have a mind of its own. The home page shows up but attempts to add additional templates or template groups with content do not display. I only get
The requested URL /template-name/ was not found on this server.
I double checked and made sure the Enable Strict URLs was set to No and the templates are all synched properly. At this point I am repeating myself. Any clues?
Edited
I found out that if I insert index.php into the URL the other pages and templates will show, which leads me to believe that I have something wrong with the htacess file.
Here is the code I am using (which has worked just fine for other sites):
# BEGIN ExpressionEngine Rewrite
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteRule ^(.*)$ /index.php?$1 [L]
</IfModule>
# END ExpressionEngine Rewrite
Any clues why this is not working correctly?
Turns out it was the htaccess file. I replaced the rewrite noted above with the following and it solved the problem:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

Godaddy: Rewrite with .htaccess

I have this htaccess setup on my clients site, they use godaddy
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
it worked fine on my MAMP but does not seem to work on the server
they have setup a subdomain like demo.example.com and the htaccess exists in the demo folder but I get 404 trying to get to page like demo.example.com/test but demo.example.com/test.php works fine so this tells me the htaccess is not work. does anyone know what I need to do to make this work ?
Try:
1) Add this line into your .htaccess: Options +FollowSymlinks
2) Add leading slash before $1, e.g. RewriteRule ^(.*)$ /$1.php [L,QSA]
P.S. There are some "rumours" that GoDaddy may not pick up changes in .htaccess file straight away.
I would also suggest to define custom 404 error handler (simple PHP script) that may help with debugging this (put this line into your .htaccess):
ErrorDocument 404 /404.php
404.php (as simple as this)
<?php
Echo "404\n";
phpinfo(INFO_VARIABLES);
The details there may help identify the issue.
UPDATE:
We have a domain name & hosting that we do not use which is about to expire. So I have logged in and tested these rules myself. Got it working straight away. Here is the setup I have:
Apache/1.3.33
Hosting Configuration: 2.1
PHP 5.x (did not bothered to check exact version)
Data Center: US Regional
.htaccess was already present and had only 1 line 9possibly left over from the site that was there originally):
addhandler x-httpd-php5 .phtml
These lines were added to .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php [L,QSA]
I have created simple php file to check if it works. Called it echo.php. Then I have tried these URLs:
http://www.example.com/abcd -- saw error page (as expected)
http://www.example.com/echo -- saw my page -- echo.php (as expected)
The full physical path to the echo.php is: /home/content/b/u/y/buyxxxxxxx/html/echo.php; ftp/website root is /home/content/b/u/y/buyxxxxxxx/html/
Try this simple setup. DO not do anything complex until you solve this issue.
This is right way of play with .htaccess on godaddy
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php [L,QSA]
This answer on Stack Overflow might help you aswell:
Add this:
Options +FollowSymLinks -MultiViews -Indexes
at the very top and give it a try.

Resources