mod_rewrite is preventing my form to be submitted as desired - forms

I have created a www.example.com and m.example.com. The m.example.com resides inside the htdocs/m/ folder. I am also deleting the '.php' extensions using .htaccess. I am having the below codes on the root directory:
RewriteCond %{REQUEST_METHOD} =POST
RewriteRule ^ - [L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
#mobile
RewriteCond %{HTTP_HOST} ^m\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/m(/|$) [NC]
RewriteRule ^(.*)$ m/$1 [L]
# Resolve .php file for extension-less php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
I have forms in the m.example.com. For example in m.example.com/myform.php, I have
<form method="post" action="processform.php">
.....
</form>
I want it to be submitted to the m/ folder (/m/processform.php), but now it always submit to the processform.php of the main directory. I have tried action="//m.example.com/processform.php" but no luck.
Anyone know kow to solve it? Thanks!
Update:
if the method of the form is get instead of post, the form will be submitted correctly (to /m/processform.php)

Actually your rules are skipping POST requests from all rewrite rules. You can tweak the rules to skip POST from external redirects and keep internal rewrites active/enabled for POST like this:
# Redirect external .php requests to extensionless url
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
#mobile
RewriteCond %{HTTP_HOST} ^m\.example\.com$ [NC]
RewriteRule ^((?!m/).*)$ m/$1 [L,NC]
# Resolve .php file for extension-less php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
PS: I have also simplified your 2nd rule.

It's possible the first redirect is interferring with the POST request, try changing the request regex to:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\s/+(.*?/)?(?:index)?(.*?)\.php[\s?] [NC]

Related

.HTACCESS Redirection Problem: index.php Before URL Slug

I am trying to achieve a simple http to https redirect and www to non-www. The problem is that .htaccess puts "index.php" before the url slug, resulting in a server error. Here is what happens:
WRONG BEHAVIOUR:
http://example.com/url-slug -> https://example.com/index.php/url-slug
DESIRED BEHAVIOUR:
http://example.com/url-slug -> https://example.com/url-slug
Note: I want all queries to redirect to the index.php page in the main directory, unless the requested file exists on the server. I'd like to achieve this without changing the url in the browser, which causes the server to crash.
(Objective: www -> non-www & http -> https)
CURRENT .HTACCESS SETTINGS:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
What am I doing wrong?
Your http to https 301 redirect should be at the top before other internal rewrite rules
RewriteEngine On
#http to https
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule (.*) https://%1%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Make sure to clear your browser cache before testing this change.

URL redirection to subdomain without changing URL in address

I am new to Linux and want to use .htaccess or proxy for the URL redirection leaving URL unchanged with apache2.
Now i want to redirect my main domain http://example.com/test to my subdomain http://test.example.com without changing the URL in address bar.
Kindly help me in giving me some guidelines with .htaccess or proxy or some other..
If you don't want the URL to change you either need a Reverse proxy set-up between the two virtual hosts / domains, or if example.com is just an alias, same IP and served from the same Apache virtual host, you could use an internal rewrite. More info HERE
your can also try this. for more infor go HERE
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^/?(index.\php)?$ /category [L]
This only works for requests for http://domain.com/ or http://domain.com/index.php, and you'd get served the content at http://domain.com/category without changing the URL in the address bar. You can probably even lose the RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC] line if you don't need it.
there is also a helpful resource here
RewriteEngine on
RewriteBase /
######## example.com ########
RewriteCond %{HTTP_HOST} ^.*example.com$ [NC]
RewriteCond /www/quux-foo.com/www/root/v1%{REQUEST_URI} -d
RewriteRule ^(.*[^/])$ %{REQUEST_FILENAME}/ [L]
RewriteCond %{HTTP_HOST} ^.*quux-foo.com$ [NC]
RewriteCond %{REQUEST_URI} ^/v1/
RewriteRule ^(.*)$ - [F]
RewriteCond %{HTTP_HOST} ^.*example.com$ [NC]
RewriteCond /www/quux-foo.com/www/root/v1%{REQUEST_URI} -f [OR]
RewriteCond /www/quux-foo.com/www/root/v1%{REQUEST_URI} -d
RewriteRule ^(.*)$ v1/$1 [L]
RewriteCond %{HTTP_HOST} ^.*example.com$ [NC]
RewriteCond %{REQUEST_URI} !^/v1/
RewriteRule ^(.*)$ v1/index.php [L]
######## example.com ########

Wordpress Redirect subdirectory to subdomain

We've just migrated a WPML site from example.com/fr to fr.example.com.
I'd like to keep all the link juice, so I'm trying to redirect all links that are
www.example.com/fr/mylink -> fr.example.com/mylink/
Here is what I have right now, but it is not working:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^([^.:]+\.)*example\.com/fr\.?(:[0-9]*)?$ [NC]
RewriteRule ^(.*)$ http://fr.example.com/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
The site works fine still but the redirects are not working. Any ideas?
Try Accessing the website via FTP and
Try adding the Code into Your wp-config.php file
define('WP_HOME', 'http://www.example.com');
define('WP_SITEURL', 'http://www.example.com/wordpress');
This Worked for me.
Try it on your website.

htaccess https redirect only on specific Zend Frameworks controller/actions

I'm new to this community, but I've found it really useful time to time.
I've searched for the answer a lot, but I didn't find anything like I need. I've tried to solve it on my own, but I still get errors, so I hope to find someone that can show me the right way... :-)
I've got a "classic" ZF website, with many controller/action urls that are redirect to index.php with a .htaccess file.
Now, what I need, is to redirect a couple of controller to https ssl connection excluding some actions of both controllers.
The way I was trying to do it is:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain\.tld [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond $1 ^!((member|shop)/(?!(index|login))(.*))
RewriteRule ^(.*) http://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteCond $1 ^((member|shop)/(?!(index|login))(.*))
RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !\.(js|ico|gif|jpg|png|css|swf|pdf|txt)$ index.php
It seems to work when I go to /member controller
But then, when I go to another controller, for example /index or /about it does not redirect to the http connection (port 80), and if I try to change a little the rewrite condition regex it sometimes does a redirect loop and the browser gives me a notice blocking the connection to the site.
Is there anyone that could show me the right synthax to use in my rewrite conditions to allow both the controllers (excluding the given actions) under an https connection and going back to a standard http connection when changing controller?
Thanks in advance.
Alessandro
Try these rules instead (replace appropriate lines):
RewriteCond %{HTTPS} on [NC]
RewriteCond %{REQUEST_URI} !^/(member|shop)/ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTPS} off [NC]
RewriteCond %{REQUEST_URI} ^/(member|shop)/ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
These rules a bit simple (HTTPS will be applied to ALL URLs in /member/ and /shop/ controllers (e.g. /member/login, /member/dashboard, /member/orders/15423/invoice etc)
Negate ! should be before ^ in RewriteCond directive -- if you want your own rules then replace RewriteCond $1 ^!((member|shop)/(?!(index|login))(.*)) by RewriteCond $1 !^((member|shop)/(?!(index|login))(.*))
A method we use to redirect to https is to leave the default Zend Framework .htaccess settings and create an action helper to redirect to https when required. Our action helper code is:
<?php
class RequireSSL extends Zend_Controller_Action_Helper_Abstract
{
public function direct()
{
if(empty($_SERVER['HTTPS']) && $config['billing']['requireSSL'])
{
$redirector = $this->getActionController()->getHelper('Redirector');
$redirector->goToUrlAndExit('https://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
}
}
}
Then when you are in a controller action that you need to be accessed via HTTPS you simply call this function at the beginning:
$this->_helper->requireSSL();
Also, here is another method for using action helpers that is a little more detailed if you need it:
http://juriansluiman.nl/en/article/110/in-control-of-https-for-action-controllers
Hope that helps!
I have discovered where the origin of the problem is, but I'd still need support to understand how to solve it.
I have tested on a local linux machine the htaccess and the result was the same... testing separately the two https condition statements (on and off) they work correctly redirecting basing on the given RewriteCond regex. When putting together only the redirect from http to https works.
Https to http redirect works only if the regex is not matched, else it redirects to http://www.mydomain.tld/index.php
So I finally tried to delete the last htaccess statement and it started to work correctly, but, obviously, it does not find the url, as it does not redirect to the index.php anymore.
It looks like after the correct https redirect the index.php one creates the problem. So I'm asking myself if there is a way to avoid this and make it work correctly.
As I wrote before, this seems to be a common problem of this htaccess, as its behaviour is the same on the test and on the production server (different linux flavours).
I put here the working code:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
#RewriteCond %{HTTP_HOST} ^mydomain\.tld [NC]
#RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond $1 !^((member|shop)/(?!(index|login))(.*))
RewriteRule ^(.*) http://%{HTTP_HOST}/$1 [L,R=302]
RewriteCond %{HTTPS} off
RewriteCond $1 ^((member|shop)/(?!(index|login))(.*))
RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [L,R=302]
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule !\.(js|ico|gif|jpg|png|css|swf|pdf|txt)$ index.php
Finally I have solved it! I persevered in searching the answer because I think that doing it with .htaccess is cleaner, smarter and easy to maintain.
The problem was essentially due to the regexp used in the "ssl to non-ssl" block that was not correctly matching the value passed (that is best matched now reading the env variable %{THE_REQUEST}, avoiding, in some cases, an erroneus redirects loop.
I paste here the working code for further reference:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteOptions MaxRedirects=1
RewriteCond %{HTTP_HOST} ^yoursite\.tld [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /((controller1|controller2)/(?!(action1|action2))(.*))\ HTTP/
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} !^[A-Z]+\ /((controller1|controller2)/(?!(action1|action2))(.*))\ HTTP/
RewriteRule ^(.*)$ http://%{SERVER_NAME}/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} \.(js|css|ico|gif|jpg|png|swf|txt|pdf)$ [OR]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [NC,L]

How do I get my contact form to work after mod_rewrite?

I've built a site using CMS Made Simple. I have to point the host URL for everything EXCEPT the admin page and the contact forms. I was able to successfully do this with mod_rewrite to the config.php and changing up the .htaccess, but now my contact forms are no longer working (one in the footer and one on the contact page). Here is what my .htaccess file looks like:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^energyfa.ipower.com$ [NC]
RewriteCond %{REQUEST_URI} !^/ai/admin/
RewriteRule ^(.*)$ http://accimpress.com/$1 [R=301,L]
# 301 Redirect all requests that don't contain a dot or trailing slash to
# include a trailing slash
# except for form POSTS
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_METHOD} !^POST$
RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]
# Rewrites urls in the form of /parent/child/
# but only rewrites if the requested URL is not a file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
Is there a reason my POST exception might not be working? Any suggestions will be much obliged. The website is: http://energyfa.ipower.com/ai/
Thanks,
Cory
try changing your .htaccess from
# .htaccess for CMS made simple
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_METHOD} !POST$
RewriteRule ^(.*) %{REQUEST_URI}/ [NE,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
# EOF
to
# .htaccess for CMS made simple
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !$ <--- to put the "/" ---->
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_METHOD} !POST$
RewriteRule ^(.*) %{REQUEST_URI} [NE,R=301,L] <--- to put the "/" after } ---->
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
# EOF
it work for us
I see that the action on your form is set to http://energyfa.ipower.com/ai/contact-us/. What I believe will happen is that the POST will go to that URL. You will hit your first RewriteRule and get redirected to the contact form again, but since it's a browser redirect, it will probably just be a GET, so your POST content never makes it to your script.
Try changing the form action to http://accimpress.com/contact-us/ and see what happens.
It looks like CMS Made Simple doesn't support their Form Builder module once you've redirected your URL with Pretty URLs, so my solution for now was using Send This File, like so:
http://accimpress.com/upload/
But thanks for your suggestions Andrew!