My .htaccess file is as follows:
Options -Multiviews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php
The issue that I am having is that when I try to access my site without the www. prefix, the .php extension is added to the address, which can often cause a problem. For example, if I try to access my homepage with the address example.com, that address is transformed into www.example.com/.php. I want the www. to be added, but the .php extension added at the end just causes an error. How do I fix this?
I believe the problem is with your last line. Mod Rewrite does not stop after executing the first rule. Try adding the last rule modifier to the redirect rule:
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
You need to tell apache to stop processing rules after the 301 redirect, do this by adding an L - e.g.:
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
aside: you'd get on better if you enabled multiviews on the first line, and removed the last 3 lines ;)
Remove below line from .htaccess and try..
RewriteRule ^(.*)$ $1.php
Related
What do i want to do is if the url have www.example.com/index.php/anything then it should throw the user to www.example.com/error-404
Here is my current expressions in my htaccess file.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule (.*)$ http://www.example.com/$1 [L,R=301]
Redirect 301 /online-features/key-anywhere-web-based-system http://www.example.com/online-features
Redirect 301 /home http://www.example.com/
Thanks in advance.
Please suggest/edit my question if i have not asked the question in correct way.
EDIT
I don't want to loose the /index.php to / redirection.
www.example.com/index.php/anything
/anything here is additional path information (after the name of a physical file). You can use the AcceptPathInfo directive to specifically disable this "feature":
AcceptPathInfo Off
In which case all URLs containing additional path info will automatically trigger a 404. However, the URLs can still be routed using mod_rewrite, which is probably what's happening here.
You will still need to implement a mod_rewrite redirect as mentioned in the datascript's answer. Or try something like the following, before your existing directives:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php/ /error-404 [R=302,L]
The check against REDIRECT_STATUS is to avoid a rewrite loop, since your front controller appears to use path info to route the request.
Change this to a 301 if this is intended to be permanent, once you have confirmed it's working OK.
However, it would be preferable if this was implemented as a proper Apache error document. For example:
ErrorDocument /error-404.php
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php/ - [R=404,L]
I think the following should do what you require. The dot is any character and the + means one or more times.
This would require the forward slash to be there
RewriteRule ^index\.php/(.+)$ error-404 [L]
EDIT: Thanks #DocRoot, updated accordingly
Here are the rewriterules for .htaccess. But I am facing a problem.
Here is my .htaccess file.
RewriteEngine On
RewriteBase /dashboard/sample_proj/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule t([\d]+)/ threads/$1/ [R=301]
RewriteRule f([\d]+)/ forums/$1/ [R=301,L]
RewriteRule t([\d]+)-([\d]+)/ threads/$1/page-$2 [R=301,L]
RewriteRule t([\d]+)/#post([\d]+) threads/$1/#post-$2 [R=301,L]
But this is a problem because there is a priority conflict between the urls.
For Example
https://www.url.com/t3550/#post44859
redirects to https://www.url.com/threads/3550/#post44859 but should instead should redirect to https://www.url.com/threads/3550/#post-44859 (this is because there is a conflict between the first and the fourth rule).
Fragments, the things after #, are not sent to the server. They're purely local in the browser, and only evaluated by the browser. A rule matching on # can never match, because the server will never get to see a URL fragment.
I have a rewrite condition in my .htaccess file which removes the need for .php file extension
RewriteEngine On
RewriteRule ^([^.]+)$ $1.php
so http://site.com/blog opens http://site.com/blog.php
but if old users type /blog.php it will also load the page
is there a way to prevent or redirect pages with .php or any other file extention to the one without it?
i mean if user entered /blog.php or /blog.asp it should either fail to load or redirect to /blog (without extention)
A better way to accomplish this would be to only rewrite if a .php by that name exists. Otherwise throw 404 for the original URL. The second set of rules would take care of removing the extension and avoiding the redirect loop.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{THE_REQUEST} ^(?:GET|POST)\ /.*\.php\ HTTP.*$ [NC]
RewriteRule ^(.*)\.php$ $1 [R=301,L]
RewriteEngine On
RewriteRule ^([^.]+)$ $1.php [L]
RewriteRule ^(.*?)\.php$ $1 [R=301,L]
You can use the rule
RewriteRule ^(.+)\.php$ $1 [R=301,L]
This would cause apache to send back a redirect to the browser which would update it's URL to the one stripped from the extension. But make sure to place this rule in front of the one the redirects to .php internally
Try this:
RewriteRule ^(.*?)\.([a-z0-9]+)$ $1 [R=301,L]
I am looking for some guidance when working with a maintenance mode system, utilised within a .htaccess file, via mod_rewrite and passing a $_GET along with it.
Passing a $_GET in mod_rewrite is perfectly fine under normal circumstances for me, but in this instance I am experiencing issues..
The code I have for the maintenance system is as follows:
RewriteCond %{REMOTE_HOST} !^XX\.XXX\.XXX\.XXX
RewriteCond %{REQUEST_URI} !/maintenance.php$
RewriteCond %{REQUEST_URI} !^/css/.*$
RewriteCond %{REQUEST_URI} !^/assets/.*$
RewriteRule ^$ maintenance.php [R=302,L]
So what I need is the ability to pass a $_GET along with the rewriteRule, thus when the site is viewed as normal by anyone using the allowed IP, I can define that the site is being viewed in maintenance mode.
Of course people not on the allowed IP get redirected to maintenance.php file and don't need this reminder anyway, as the page does that already.
Thank you in advance anyone that can help me in this issue.
EDIT::
# Start the mod re-write conditions #
RewriteRule ^([^/]+)\/$ ?cat=generic&page=$1 [L]
RewriteRule ^products\/([^/]+)\/([^/]+)\/$ ?cat=product&page=$2 [L]
RewriteRule ^theteam\/([^/]+)\/$ ?cat=staff&page=$1 [L]
Thats how I deal with the other links on my page, I hope that is all you needed to see.
Dan.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !maintenance.php
RewriteCond %{REQUEST_URI} !(css|assets).*$
RewriteCond %{REMOTE_ADDR} !^XX\.XXX\.XXX\.XXX$
RewriteRule (.*) /maintenance.php [R=302,L]
RewriteCond %{REMOTE_ADDR} ^XX\.XXX\.XXX\.XXX$
RewriteRule (.*) $1?mode=maintenance [L,QSA]
This works for me, first you have the negation redirect so whoever tries to access without the allowed IP will go to maintenance.php.
Then you have the append internal redirect, the GET mode=maintenance will not be visible but will be there.
And you can retrieve it with $_GET['mode'].
If you want it to visually append to the query string and only to php files you can use:
RewriteCond %{REMOTE_ADDR} ^XX\.XXX\.XXX\.XXX$
RewriteCond %{REQUEST_FILENAME} \.php$
RewriteCond %{QUERY_STRING} !^mode=maintenance.*$
RewriteRule (.*) $1?mode=maintenance [R,L,QSA]
Update of visible rule for your sub directories format this rule should be placed before it like this:
RewriteCond %{REMOTE_ADDR} ^XX\.XXX\.XXX\.XXX$
RewriteCond %{QUERY_STRING} !.*mode=maintenance.*$
# for visible query string uncomment the below line and comment the next rule
#RewriteRule (.*) $1?mode=maintenance [R,L,QSA]
# for invisible query string
RewriteRule (.*) $1?mode=maintenance [L,QSA]
# Start the mod re-write conditions #
RewriteRule ^([^/]+)\/$ ?cat=generic&page=$1 [L,QSA]
RewriteRule ^products\/([^/]+)\/([^/]+)\/$ ?cat=product&page=$2 [L,QSA]
RewriteRule ^theteam\/([^/]+)\/$ ?cat=staff&page=$1 [L,QSA]
My .htaccess file is as follows:
Options -Multiviews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php
It works, but I'm wondering how it works. For example, if I type in example.com/main, I get the file at www.example.com/main.php. How do I get the .php extension if the code tells the rewriting to stop after adding the www. to the beginning of example.com?
Edit: Or should I create a unique ID only for the purpose of logging in the remembered user?
The particular behavior you're asking about comes about because the rule
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
is a 301 redirect; it instructs the browser to initiate a completely new HTTP request. The L only causes (can only cause) it to be the last rule executed for that request; the new request comes in with the correct hostname and proceeds onward.