I'm using the below in my htaccess file to strip out the .php extensions on my files. It works fine.
However, I also want to utilize ErrorDocument 404 /index.php to redirect any non existing urls back to the homepage. If I use ErrorDocument 404 /index.php alone in the htaccess, the redirects work fine. Also, if I use the below Rewrite code it successfully strips the php extensions. I would like both of these to work together, however.
For example, if a user enters "www.domain.com/register", they go to www.domain.com/register.php. But if they enter "www.domain.com/something", and something.php doesn't exist, then they redirect back to index.php. I've tried every permutation of both of these together in my htaccess to no avail. Any help would be appreciated.
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]
Not sure what the problem you're having is exactly, but if I change your rules to this, it works for me:
ErrorDocument 404 /index.php
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
So instead of checking if the request isn't accessing an actual file/directory that exists, check that the .php version of that file DOES exist.
Related
Stuck again with htaccess. How can I add a 404 page to a specific subdirectory?
In my case the 404 page should only affect the "staging" subdirectory (and the directories inside of staging) but not the root or any other subdirectories placed in root.
http://example.com/staging
The code which is in my .htaccess file for the directory "staging":
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]
Options -Indexes -MultiViews
RewriteEngine on
RewriteRule ^arbeiten/$ /staging/arbeiten/alle [R=301,L]
RewriteRule ^journal/$ /staging/journal/journal-alle [R=301,L]
ErrorDocument 404 /staging/404.html
Using the code above (and many others) results in a Internal Server Error.
Update #1 to my initial question.
In general, the code/answer provided to add a 404 not found page to a specific sub directory works:
ErrorDocument 404 /staging/404.html
The reason why it still does not work is because there must be some error in the overall .htaccess setup related to the code remove .html extension and/or redirect to a page in a folder.
Options -Indexes -MultiViews
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L]
RewriteRule ^arbeiten/$ /staging/arbeiten/alle [R=301,L]
RewriteRule ^journal/$ /staging/journal/journal-alle [R=301,L]
ErrorDocument 404 /at/404.html
I will continue to further investigate the setup in my .htaccess.
Update #2
Using the .htaccess script below allows to remove .html extensions, redirect pages to a specific page within a directory and basically a working 404 not found page within a specific sub directory on a Hostgator cPanel grid hosting (apache).
Options -Indexes -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]
RewriteRule ^arbeiten/$ /staging/arbeiten/alle [R=301,L]
RewriteRule ^journal/$ /staging/journal/journal-alle [R=301,L]
ErrorDocument 404 http://www.example.com/staging/404
Remaining 404 problem:
http://example.com/staging/blabla (404 works)
http://example.com/staging/folder/blala (404 works)
http://example.com/staging/folder/project-detail-pagexxx (404 works)
http://example.com/staging/folder/project-detail-pagexxx/ (404 does not work)
how to write RewriteRule to access php file as html extension, the mark file is located inside folder called domain.com/markfolder/mark.php
and i got following in my root directory
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RedirectMatch 301 (.*)\.html$ $1.php
and below file i try to locate inside domain.com/markfolder/.htaccess
try 1
RewriteEngine on
RewriteRule ^mark\.php$ mark.html [NC,R]
try 2
RewriteEngine on
RewriteRule ^(.*)\.html $1\.php
try 3
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^(.+)\.html$ $1.php [NC,L]
above three try simple doesn't work.
First, your htaccess file in your root directory is causing the problem. It's redirecting .html to .php and is part of mod_alias, not mod_rewrite. This means requests will get processed by both the RedirectMatch as well as any rewrite rules. So first change your root htaccess to:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\.html$ $1.php [L,R=301]
Not sure why you want to permanently redirect, since this will affect all requests even requests in subdirectories. It seems like it's not what you want and will only break any attempt for you to rewrite.
Then in the htaccess file in your markfolder:
RewriteEngine On
RewriteRule ^(.*)\.html$ $1.php [L]
I have a directory structure like root/user/confirm/index.php
I want to make redirect like
Redirect this url
http://www.site.com/user/confirm/ASd2sasda4ass
To this
http://www.site.com/user/confirm/index.php?confirm=ASd2sasda4ass
I am trying this way
RewriteRule /user/confirm/([^A-Za-z0-9])$ /user/confirm/index.php?confirm=$1 [L,QSA]
It redirects this url properly
http://www.site.com/user/confirm/index.php/ASd2sasda4ass
To
http://www.site.com/user/confirm/index.php?confirm=ASd2sasda4ass
But not working for this url without index.php
http://www.site.com/user/confirm/ASd2sasda4ass
It shows 404 not found error
Please see and suggest any possible way to do this.
Thanks.
UPDATE
Complete codes
Options +FollowSymlinks
RewriteEngine on
<IfModule mod_rewrite.c>
RewriteRule /user/confirm/([^A-Za-z0-9])$ /user/confirm/index.php?confirm=$1 [L,QSA]
</IfModule>
Try this (without confirm.php in the url. You shouldn't need it anyway):
RewriteRule ^user/confirm/(.*)/? user_confirm.php?confirm=$1 [L]
I solved this issue by removing this rewrite rule from root .htaccess
RewriteRule ^/user/confirm/([^A-Za-z0-9])$ /user/confirm/index.php?confirm=$1 [L,QSA]
And placing another .htaccess file in confirm directory to hide index.php and processing query parameter there with following codes.
Options +FollowSymlinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?confirm=$1 [L]
</IfModule>
And now its working without index.php in url. Hope this helps others too with same issue.
I have a website built on codeigniter and am trying to get the PP IPN working with it. I have looked at a few CI specific libraries, but want to have a stand alone IPN file.
Basically, I have CI installed in the website root and I have another directory 'pp', also in the root.
I'm trying to test the IPN using Paypal's sandbox, but it's returning a 404, I also can't access the file directly.
Here is my .htaccess file:
XSendFile on
Options -Indexes
Options +FollowSymlinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
### Canonicalize codeigniter URLs
RewriteRule ^(index(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
RewriteCond %{HTTP_HOST} !^(www) [NC]
RewriteRule ^(.*)$ http://www.mysite.net/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|pp|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php
</IfModule>
Here's hoping it's something really obvious!
Thanks,
Ryan
If the pp directory exists, you could just put an htaccess file in that directory and just have:
RewriteEngine On
in it by itself, and any rules in the main htaccess file would get ignored. If your paypal stuff already has rewrite rules in an htaccess file in the pp directory, you may want to look into those rules as the cause of the 404.
Do you have mod_rewrite.c enabled. If not, that .htaccess will always give 404. (Sorry I couldn't make this a comment, don't have the rep).
I have this rule in my .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php
what it does is to redirect all url's index.php?somevar=# to index.php, however when it redirect's sends out code 302 and i want it to be 301.
I tried to add [R=301] after RewriteRule . /index.php but it doesn't work.
Some help is appreciated :-)
This thing does not do what you describe. Your rule sends every request to non-existent files to index.php. The URL:
index.php?anything
already calls index.php. You don't need any rewrite magic to handle that.
Also ...
RewriteRule . /index.php
is a bad practice. It does not cause you problems now, but it could in the future, when you modify the rules. You'd rather use:
RewriteRule .* /index.php