Here is my htaccess
Options -Indexes
<IfModule dir_module>
DirectoryIndex in.php
</IfModule>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule \.(css|jpe?g|gif|png|js|css|htm|html|mp3|wav|ico)$ - [L]
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php http://www.example.com/$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^some/(.*)/(.*)/$ test.php?z=$1&n=$2 [NC,L]
RewriteRule ^some/(.*)/(.*)$ test.php?z=$1&n=$2 [NC,L]
RewriteRule ^some/(.*)/$ test.php?z=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !.*test.php$ [NC]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
None of the $_POST with relative paths are working after implementing mod_rewrite.
Note :I have already set
< base href="http://example.com/"/>
and RewriteBase / as mentioned above
I have gone through other questions at SO but no luck!
Thanks for your advise!
I'm sorry.. Not working in the sense, POST variables are probably getting lost during htaccess redirects ???
I would validate the code you are using to build your form actions. I agree with the above commentors that you are losing your POST variables because of the redirect.
Why might this be happening?
First off, that rewrite rule to redirect from non-www to www should only happen the first time someone visits your website without the www's. You should program accordingly.
Ie - someone types in 'example.com' - your rewrite will take them to 'www.example.com'
From then on, if you must refer to your full domain in a form, use the entire WWW version.
<form action="http://www.example.com/processForm.php" method="POST">
Alternatively, you could stop using the full domain.
A common place I've seen this happen is when programmers use plugins that define a configuration object. Perhaps that configuration object is pointing to the non www version by accident. Imagine this code:
$config = array('domain'=>'example.com', 'othersettings'=>'more here');
Forms are sometimes created doing this:
<form action="<?php echo $config['domain'] ?>/processForm.php" method="POST">
See, that would get translated into the non-www version, losing your POST values.
Good luck!
Related
I have a site with 3 scripts, namely viewgames.php, play.php and users.php present at the root directory. I also have a .htaccess file in the same (root) directory.
My .htaccess code that cleans URLs is :-
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^search/(.*)$ ./viewgames.php?search=$1
RewriteRule ^category/(.*)$ ./viewgames.php?cat=$1
RewriteRule ^users/(.*)$ ./users.php?action=$1
RewriteRule ^play/(.*)$ ./play.php?gn=$1
RewriteRule ^([a-z]+)\/?$ $1.php [NC]
ErrorDocument 404 /pagenotfound.php
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
When I was developing in localhost (XAMPP), I got no problem in accessing the clean URLs (using the above htaccess code) such as localhost/MySite/play/foo, localhost/MySite/category/action, localhost/MySite/search/lolol or localhost/MySite/users/myaccount.
It clearly means that the above .htaccess code was working like a charm in my localhost server. Then I uploaded the above code to a shared Apache-based linux server, and encountered some weird problems -
https://example.com/category/blabla was working fine, and so was https://example.com/search/blabla
But the URLs https://example.com/play/xyz and https://example.com/users/anyaction stopped working. By that I mean, the page was loading fine, the site was there, but no $_GET values were passed to the PHP script. Thus, I am not able to get the value of parametes gn and action. (see above code)
I even tried by var_dump-ing the $_GET array in the scripts, and they returned an empty array, meaning that no values were sent to the script.
Please check my .htaccess code. I am completely and utterly new to .htaccess, and the above code is merely a copied one. I will really appreciate any help in the matter.
Have it like this:
ErrorDocument 404 /pagenotfound.php
Options -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteRule ^search/(.*)$ viewgames.php?search=$1 [L,QSA,NC]
RewriteRule ^category/(.*)$ viewgames.php?cat=$1 [L,QSA,NC]
RewriteRule ^users/(.*)$ users.php?action=$1 [L,QSA,NC]
RewriteRule ^play/(.*)$ play.php?gn=$1 [L,QSA,NC]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-z]+)/?$ $1.php [L,NC]
It is important to keep MultiViews options off to avoid content negotiation feature causing issues for mod_rewrite module.
I am currently working on a project where I have to redirect a user after a form has been submitted.
Once the form is submitted I redirect the user to a url based on what they entered into the form. An example of what the URL might look like:
/oslo-norway-copenhagen-denmark
I would like /oslo-norway-copenhagen-denmark to display in the users URL bar, would would like it to represent the page /page.php?id=oslo-norway-copenhagen-denmark.
I've currently got this working:
Options +FollowSymLinks
RewriteEngine On
DirectoryIndex search.php
RewriteBase /services
RewriteRule ^formmail/([^/]*)$ formmail.php?id=$1 [L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
The only problem is that this makes /formmail/oslo-norway-copenhagen-denmark work, but how do I go about making this only /oslo-norway-copenhagen-denmark? I tried removing the /formmail/ but this didn't work
Could anyone help me solve this problem? Thank you very much!
All you need to use is this in your .htaccess file:
RewriteEngine On
RewriteRule ^([^/]*)$ /services/page.php?id=$1 [L]
Make sure you clear your cache before testing it. It will leave you with your desired URL.
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
I am trying to make SSL and Redirection work perfectly with my web application. I wish to achieve that always https://www.mydomain.com/ should be loaded in the browser - although, if he types a subdomain - it should be redirected to https://subdomain.mydomain.com/ instead.
I mean to say, everything should be SSL - here is what I am doing currently
Options -Indexes
Options +FollowSymLinks
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www) [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule (.*)\.php$ index.php/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
Also, I wish to improve this .htaccess file so to introduce more security measures plus also allow only js, css and images files to be accessible by everyone - rest everything hidden or redirected to a 404 page.
Please guide, I would be greatly thankful!
These rewrite conditions may help you, I use this to make my CodeIgniter go to HTTPS:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]
I'm building a small local social good community website where every project has his own url, such that when username='foo' the URL becomes http://website.com/foo similar to Twitter. Even though I'm not experienced with htaccess I've managed to make it work to a large extent. However I am now running into problems when someone types in http://website.com/subdomain/foo. This now also tries to find the username and display the page, but I don't want that to work and want to show them our 404 page.
Also, on some subdomain's (i.e. about) we have other pages such as 'ourstory.php'. I'd really like to display 'about/ourstory' instead of about/ourstory.php, but only for selected subdomains.
Finally, when having the structure 'website.com/subdomain/subdomain', it generates internal server errors when requiring other php files, even when absolute paths are used.
Here is my current mod_rewrite, I hope you can understand what I try to do and feel free to optimize, I've probably made some beginner mistakes.
This mod rewrite does three things:
1. Remove 'www' infront of address;
2. The second makes sure that when they just type the homepage, it takes them to the homepage(removing this, would result in an empty homepage for some reason, can this be done in another way?);
3. When a name is typed that isn't a subdomain, it redirects them to the profile page and makes the url the username.
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/$ http://website.com/$1 [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|gif|ico|jpg|js|png|swf|txt)$
RewriteRule ^(.{5,16})$ profile.php?scrname=$1
RewriteRule ^(.+)(\.php|\.html)/$ /$1$2 [R=301,L]
</IfModule>
Again, any help will be greatly appreciated.
You might find it easier to maintain if you use a front controller instead. Basically, you would redirect all requests that aren't for static files to a single php script, which can then perform advanced rules more easily expressed in code than mod_rewrite rules.
Your .htaccess might then look a little like:
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^(.*)$ index.php?q=$1
In your PHP code you could then process this by examining $_GET['q'] to decide which page to render.
I have made some progress since I posed the question.
Here is the (seemingly very simple) mod_rewrite I ended up with:
<IfModule mod_rewrite.c>
//mod_rewrite by Marc
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(\w+)$ ./project.php?id=$1
</IfModule>
It does everything I wanted, fixed the internal server errors and therefore I don't need the rewrite to remove the .php anymore. I hope it can help someone else.