htaccess maintenance page with a url variable exception - regex

I searched for a while, but couldn't find the exact answer. So here it goes.
I have a membership site that has a PayPal IPN listener. This needs to be active and accesable at all times!
I am doing a maintenance update today that might take a few hours. How do I add an execption for a URL like this in my .htaccess page:
http://mycoolwebsite.com/?listener=IPN
Here is my .htaccess page so far:
# activate rewrite engine
RewriteEngine On
# ip address so i can access
RewriteCond %{REMOTE_ADDR} !=xx.xx.xx.xx
RewriteCond %{REQUEST_URI} !^/img/.*$
RewriteCond %{REQUEST_URI} !^/maintenance\.php$
Thanks NINJAS!

You can have your rules like this:
# activate rewrite engine
RewriteEngine On
# skip /?listener=IPN from rewrites
RewriteCond %{QUERY_STRING} ^listener=IPN$
RewriteRule ^ - [L]
# rest of your rules follow
PS: I notice that you just have 3 RewriteCond lines without any RewriteRule line. Which is not really doing anything for you.

Related

htaccess exception for in-page anchor links

I use directives in an .htaccess file to clean-up my website URLs.
For instance, this directive adds a trailing slash:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
and these remove file extensions:
# hide .php file extensions
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
# redirect .html to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L]
# redirect .htm to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.htm [NC]
RewriteRule ^ %1 [R,L]
The problem I'm having is with in-page anchors. When I create a link to a section of page, like this:
https://www.mywebsite.com/privacy-policy#information-we-collect
The system outputs this:
https://www.mywebsite.com/privacy-policy/#information-we-collect
How do I adjust the .htaccess file to make an exception for the trailing slash requirement when it comes to in-page anchor links?
A related problem is this:
Although both of these links work:
https://www.mywebsite.com/privacy-policy#information-we-collect
https://www.mywebsite.com/privacy-policy/#information-we-collect
... when they are inside the page (Privacy Policy, in this case).
The URL never adjusts in the address bar to show the fragment identifier (#...). The address bar stays fixed at:
https://www.mywebsite.com/privacy-policy/
When it would normally adjust to:
https://www.mywebsite.com/privacy-policy#information-we-collect
Lastly, while these links with fragment identifiers work within their page, a link with a fragment identifier to another page, does not work.
So, if I'm on the Privacy Policy page, all these links work fine:
https://www.mywebsite.com/privacy-policy#information-we-collect
https://www.mywebsite.com/privacy-policy/#information-we-collect
https://www.mywebsite.com/terms-of-service/
But this is totally unresponsive:
https://www.mywebsite.com/terms-of-service#limitation-of-liability
It only works within the Terms of Service page.
Converting my comments to an answer.
This exception cannot work:
Because a web server (or rewrite rules) don't know anything about anchors because part of URL starting with # is completely handled in browser and is not sent to web server.
#information-we-collect part won't be sent to web server so creating an exception for #... won't really solve the problem. There has to be some other way to create exception e.g. adding a prefix/suffix or a dummy query parameter that rewrite rules can see and take action on.
Alternatively you can handle it on client side itself i.e. inside Javascript code.

RewriteCond when REQUEST_URI do not match htaccess apache2

I have a multilingual wordpress website and want to redirect website of given region to given language,
xyz.de --> xyz.de/de/
xyz.co.uk --> xyz.co.uk/en/
direct access to xyz.de/de and xyz.co.uk/en are working properly. So there is no problem on wordpress side.
Now, I am trying to change the htaccess file of xyz.de and xyz.co.uk so that they redirect the website.
Considering xyz.co.uk
I want to add a RewriteCond such that whenever there is no /en trailing after xyz.co.uk it will automatically add /en.
For example xyz.co.uk/<trailing address> results in xyz.co.uk/en/<trailing address>
So far I have the following code, which somehow doesn't seem to work,
RewriteCond %{REQUEST_URI} !^/en
RewriteRule ^(.*)$ http://xyz.co.uk/en/$1 [L]
The negation of /en is not working! I have also tried
RewriteCond %{REQUEST_URI} !/en
RewriteRule ^(.*)$ http://xyz.co.uk/en/$1 [L]
Could someone tell me where I am going wrong? seems like I have gone wrong in writing RegEx and suggest if there is better way to achieve the same, that does not affect the SEO across different domains.
Use THE_REQUEST variable instead of REQUEST_URI:
RewriteCond %{HTTP_HOST} \.co\.uk$ [NC]
RewriteCond %{THE_REQUEST} !/en/ [NC]
RewriteRule ^ /en%{REQUEST_URI} [L,R=302,NE]
Make sure to keep this rule as your very first rule in .htaccess.
Change it to R=301 once you've tested.

Wordpress: htaccess redirect (301) doesnt work

I have a task which drives me nuts: I have an old blog and want to create a 301 redirect to my new blog for several posts because I moved some of the content to the new domain.
Let's say one of my old posts is to be find at example.com/this-is-my-post and visitors should be redirected to example.org/my-old-post-2817.
What I tried is this:
# BEGIN Redirect
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^this-is-my-post$ http://example.org/my-old-post-2817 [R=301]
</IfModule>
# END Redirect
The problem is that there is no redirect in FF (desktop), Chrome (Android) and standard internet browser (Android). In IE (desktop) I get an error stating that the page can't be displayed. No reason is given.
I made sure not to have any unneccessary blank lines in the .htaccess and, to be on the safe side, deleted my browser's histories, caches and so on.
A CCA of my provider dropped me a message that he re-saved the mod rewrite module and that I have to try it 15 minutes later again. With no success.
So, what did I wrong?
Regards,
Crunchy
Change order of rules to keep R=301 rules before internal WP rules:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^this-is-my-post/?$ http://example.org/my-old-post-2817 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

How to redirect from an old php script to a new php script using mod_rewrite

It's my first request here and hopefully I won't upset anyone.
Here's my story:
I have looked all over this place for a solution and wasn't able to find one. Here's hoping that someone can give some input.
I've basically managed to use Apache's mod_rewrite to create SEO-Friendly urls for my website.
E.g. Old path www.hostname/index1.php?session=user is now rewritten as www.hostname/user, which results into a SEO Friendly URL address.
However, the old path is still valid. I need to somehow redirect all the incoming old index1.php requests to the newer URLs, the SEO-Friendly ones, for the search engines to transfer the link popularities to the new ones. I believe I may have an infinite-loop redirect and that's why it's not working.
My code so far:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
## Redirect still valid old links to SEO friendly ones
RewriteRule ^/?index1\.php\?session=user$ /user [R=301]
## Catch the above and rewrite the URL
RewriteRule ^/?user/?$ /index1.php?session=user [QSA,L]
The above rules never get hit when the htaccess file is parsed.
It hit me that I might be doing some sort of redirect loop here so I thought about renaming the index1.php file to index2.php and create something like:
## Redirect still valid old links to SEO friendly ones
RewriteRule ^/?index1\.php\?session=user$ /user [R=301]
## Catch the above and rewrite the URL
RewriteRule ^/?user/?$ /index2.php?session=user [QSA,L]
However, that failed too.
What would be the best approach to this? What am I doing wrong here?
Thank you!
Update your .htaccess rules to
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
## Redirect still valid old links to SEO friendly ones
RewriteCond %{QUERY_STRING} !no-redir [NC]
RewriteCond %{QUERY_STRING} session=user [NC]
RewriteRule ^/?index1\.php$ /user? [R=301,NC,L]
## Catch the above and rewrite the URL
RewriteRule ^/?user/?$ /index1.php?session=user&no-redir [QSA,NC,L]
You can't match against the query string (everything after the ?) in a rewrite rule, so you can't match against the session= part. You also can't simply match against the %{QUERY_STRING} var because that gets populated by you other rule when it rewrites the SEO friendly URL to the one with the query string. So you need to match against the actual request:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /index1\.php\?session=([^&]+)&?([^\ ]*)
RewriteRule ^ /%2?%3 [L,R=301]

unable to come up with htaccess rewrite engine

I want to redirect domain1.com to domain2.com in all cases except one particular case:
domain1.com/subfolder/index.php
I want this domain1.com/subfolder/index.php to be intact and not get redirected to domain2.com because I have hundreds of users already bookmarked this page.
But anything and everything besides that domain1.com/subfolder/index.php, I want domain1.com to be redirected to domain2.com
Please help.
Have the following .htaccess in your web root /
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)domain1.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subfolder/index.php$ [NC]
RewriteRule ^(.*)$ http://domain2.com/$1 [R=301,L]
Use just [L] if you want the redirection to be transparent i.e. without letting your visitors know.
Use !^/subfolder/.*$ if you want to stop redirection for the complete folder as well as its contents.
You can use RewriteCond to check to see the requested uri is anything but the one you want to redirect, capture the desired path and then redirect to the second domain.
RewriteCond %{REQUEST_URI} !^/subfolder/index.php$
RewriteRule (.*) http://domain2.com/$1