RewriteCond and rules except some page - apache

Hello I want use redirect module on apache2
if request "test.net/xxx" then redirect to "test.test..net/xxx"
but except index.html page
for example if request "test.net/index.html" then no redirect
i wrote the apache2 conf file as below.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test\.net [NC]
RewriteRule ^http://test\.net/([^index\.html]) http://test.test.net/$1 [R=301,L]
but doesn't work
how to edit above sentences? please tell me
thanks!

You add a condition to take care that the rule is only applied if the exception is not met and fix the actual rewriting rule:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test\.net$ [NC]
RewriteCond %{REQUEST_URI} !^/index\.html$
RewriteRule ^/?(.*)$ http://test.test.net/$1 [R=301]
That rule will work in the http servers host configuration but also in dynamic configuration files (".htaccess").
If you receive an http status 500 ("internal server error") with above rule then chances are that you operate a very old version of the apachE http server. The error log files will then point out an unknown flag [END]. Replace it with the old [L] flag in that case.

Related

How to redirect url to the url with htaccess

I didn't know how to question this but my problem is like this
I have written a rule for a url
RewriteRule ^mysite.com.pk/([a-zA-Z0-9]+) index.php?store=$1
RewriteRule ^mysite.com.pk/([a-zA-Z0-9]+)/products index.php?store=$1&view=products
RewriteRule ^mysite.com.pk/([a-zA-Z0-9]+)/products/([0-9]+) index.php?store=$1&view=products&category=$2
RewriteRule ^mysite.com.pk/([a-zA-Z0-9]+)/single/([0-9]+) index.php?store=$1&view=single&product=$2
I am new to htaccess so I don't know much about it. Is there a way that even if a old url comes like index.php?store=abc&view=single&product=123
this will be redirected to a new one like mysite.com.pk/abc/single/123
You have to make some small modifications to your proposed rules to get them to work. And you have to add additional rewriting rules to achieve what you actually ask: redirecting "old" URLs to your new syntax.
This is probably what you are looking for:
RewriteEngine on
RewriteRule ^/?([a-zA-Z0-9]+)$ index.php?store=$1 [END]
RewriteRule ^/?([a-zA-Z0-9]+)/products$ index.php?store=$1&view=products [END]
RewriteRule ^/?([a-zA-Z0-9]+)/products/([0-9]+)$ index.php?store=$1&view=products&category=$2 [END]
RewriteRule ^/?([a-zA-Z0-9]+)/single/([0-9]+)$ index.php?store=$1&view=single&product=$2 [END]
# index.php?store=xyz&view=single&product=123
RewriteCond %{QUERY_STRING} store=([^&]+)&?
RewriteCond %{QUERY_STRING} view=single&?
RewriteCond %{QUERY_STRING} product=([^&]+)&?
RewriteRule /?index.php$ /%1/single/%3 [END,R=301]
# index.php?store=xyz&view=products&product=123
RewriteCond %{QUERY_STRING} store=([^&]+)&?
RewriteCond %{QUERY_STRING} view=products&?
RewriteCond %{QUERY_STRING} category=([^&]+)&?
RewriteRule /?index.php$ /%1/products/%3 [END,R=301]
# index.php?store=xyz&view=products
RewriteCond %{QUERY_STRING} store=([^&]+)&?
RewriteCond %{QUERY_STRING} view=products&?
RewriteRule /?index.php$ /%1/products [END,R=301]
# index.php?store=xyz
RewriteCond %{QUERY_STRING} store=([^&]+)&?
RewriteRule /?index.php$ /%1 [END,R=301]
Those rules should work the same in a .htaccess style file and in the real http host configuration. Please see the note below about that.
If you are using an old version of the apache http server then you may have to replace the END flag by the L flag. Try that if you receive an "internal server error" (http status 500) and the server complains about the END flag in the error log file. You may have to add some additional conditions to prevent an endless rewriting loop in that case.
Please note that I did not test that rule set. I hope it does not contain any silly mistakes and you certainly will have to test it.
All of the above assumes that mysite.com.pk is meant to match your host name ("domain"). But that will not work, since the RewriteRules work on the request path, not the full URL. If you want to limit the application of the rules to a specific host, then you can add a leading condition to stop the rewriting process:
RewriteCond %{HTTP_HOST} !^mysite\.com\.pk`
RewriteRule .* - [END]
And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using .htaccess style files. Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

Moving from IIS ISAPI rewrite to Apache mod_rewrite

I've seen few people have asked this on here already, but none of the solutions provided worked for me so far.
We have always been developing for the IIS ISAPI rewrite (on Windows) and suddenly one of the clients decided to place the project on the Linux server running Apache. As a result some of the rules are no longer working.
Example of an ISAPI rewrite rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^login/?([^/]*)([^/]*)$ /login.cfm?msg=$1&$2 [L,QSA]
RewriteRule ^login.cfm$ /login/ [R=301,L,NC]
login.cfm is an actual existing page and not dynamically generated based on template.
Could someone help me how to translate this for the Apache mod_rewrite please? Currently the rule creates an infinitive loop and the output is:
login/?msg=&&msg=&&msg=&&msg=&&msg=&&msg... (till the limit of the url length)
Safe to say the page is not found either so it doesn't even check whether the file with such name exists.
The page could be /login or /login/wrong so the rule should recognize both cases.
You can use these rules in your site root .htaccess:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /login\.cfm\?msg=([^\s&]*)\s [NC]
RewriteRule ^ /login/%1? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^login(?:/([\w-]+))?/?$ login.cfm?msg=$1 [L,QSA,NC]

Apache htaccess sending URLs in a URL

I'm trying to send a URL in a URL, and there's ModRewrite in the middle.
Here's the Rewrite Rule. It's used to search for a user's profile.
RewriteEngine On
RewriteRule ^(query)/(profile)/([a-z0-9:.()\-\ ]+)/?$ search/index.php?scope=$2&query=$3 [NC,L]
And this is the URL
http://www.example.com/query/profile/http://www.facebook.com/example
No matter how I do the rewrite rule, I keep getting NetworkError: 500 Internal Server Error or NetworkError: 404 Not Found if it's a URL I put there.
How can I do this right? I'm on Windows 7 Home Premium. Am I missing something?
The URL above is passed using ajax.
Edit:
#htaccess in site1 folder
RewriteRule ^resources/?(.*)/?$ /control/$1 [NC,L]
#htaccess in control folder
RewriteRule ^(query)/(profile)/([a-z0-9:.()\-\ ]+)/?$ search/index.php?scope=$2&query=$3 [NC,L]
next folder is `search` which has `index.php`
URL used
http://www.example.com/resources/query/profile/http://www.facebook.com/example
EDIT:
I redid the RewriteRule like below, and don't get any error messages now, but Apache seems to be taking off one / from the url after the http://. So I get only http:/www.facebook.com/example. Any help with that will be appreciated.
RewriteRule ^(query)/(profile)/([a-z0-9\/:.()\-\ ]+)/?$ search/index.php?scope=$2&query=$3 [NC,L]`
You need to capture URL from RewriteCond otherwise Apache will trim multiple // into a single one.
Change this rule in root .htaccess:
RewriteCond %{REQUEST_URI} /resources/(.+)$ [NC]
RewriteRule ^ /control/%1 [L]
Have this in /control/.htaccess file:
RewriteEngine On
RewriteBase /control/
RewriteCond %{REQUEST_URI} /(query)/(profile)/(.+)$ [NC]
RewriteRule ^ /resources/search/index.php?scope=%2&query=%3 [QSA,L]

Disguising URL by rewriting htaccess

I have a site built for a client located on my server (ie http://www.myserver.com/clientsite). Can I disguise the URL by modifying the .htaccess file on my client's host (http://www.clientsite.com) to read as his domain, but display my content and keep the subdirectories in tact?
So: http://www.myserver.com/clientsite would read http://www.clientsite.com
and http://www.myserver.com/clientsite/about would read http://www.clientsite.com/about
I tried the following, but it was directing me to a 404 error.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^clientsite.com
RewriteRule ^(.*) http://www.myserver.com/clientsite/$1 [P]
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?clientsite.com
RewriteRule ^(.*) http://www.myserver.com/clientsite/$1 [P]
Since you mentioned that your client's site is http://www.clientsite.com and Rewrite condition was applied only to clientsite.com , I changed it to support both with and without www.
If it doesn't work, put your rewrite log and I'll check that.

Apache RewriteCond ignored in case of https

I have to redirect from one domain to another domain. Both the domains have http and https protocol enabled. so in order to map http and https i have tried various combinations in conf file as below:
#RewriteCond %{HTTPS} =on
#RewriteCond %{SERVER_PORT} ^443$
#RewriteRule ^(.+)$ - [env=askapache:https]
#RewriteCond %{HTTPS} !=on
#RewriteRule ^(.+)$ - [env=askapache:http]
RewriteCond %{SERVER_PORT}s ^(443(s)|[0-9]+s)$
RewriteRule ^(.+)$ - [env=askapache:%2]
RewriteCond %{HTTP_HOST} ^([www.]+)?test-redirect\.com$ [NC]
RewriteRule ^(.*)$ http%{ENV:askapache}//amit.test.com/content/test/category/6 [L]
#RewriteCond %{HTTP_HOST} ^([www.]+)?test-redirect\.com$ [NC] <BR>
#RewriteCond %{HTTPS} !on
#RewriteRule .? http://amit.test.com/content/test/category/6 [L]`
But every time https condition is skipped/ignored. there is nothing rewrite logs as well. i have seen so many examples on net. but fail to understand why it is not detecting https? where http is working perfectly fine.
Rewrite logs may very well be in 2 diff locations for https and http. You can try using HTTP_AA instead of using "askapache" for the name of the env. The prefix HTTP_ANYTHING is a more fail safe way to make sure the var is available since some setups don't allow custom vars that start with anything other than HTTP_ which they have to allow due to it represents a HTTP header usually.
Make sure your https port is actually 443 or you will need to change the code.
Make sure your mod_rewrite block of code works by trying the first and second methods here: Even Newer HTTP/HTTPS Rewrite Code If it doesn't work using the first example you need to get it working using that rule first.
Try setting the HTTP_AA var above the rewrite code using the SetEnvIfNoCase directive or with
SetEnv HTTP_AA
Verify your vhost/httpd.conf settings for both SSL and non-SSL like the document root and Options and AllowOverrides, Logs and maybe StdEnvVars for SSLOptions.
Build a shtml file using mod_includes that just does a printenv. Then view both the ssl and non-ssl outputs and pay particular attention to the vars with the prefix REDIRECT_ and obviously make sure the HTTP_AA var shows up correctly in the printenv output. Or you could use the printenv cgi script or the shtml example on the askapache site.
Don't forget http://httpd.apache.org/.
Or try this
Options +FollowSymLinks +ExecCGI
# Set var now to try to try to have it availabe for mod_rewrite
SetEnv HTTP_AA
RewriteEngine On
RewriteBase /
# HTTP_AA is set to either s or is blank
RewriteCond %{SERVER_PORT}s ^(443(s)|[0-9]+s)$
RewriteRule ^(.+)$ - [env=HTTP_AA:%2]
# if host not new domain, its old so redirect both http/https to new
RewriteCond %{HTTP_HOST} !^amit\.example\.com$ [NC]
RewriteRule .* http%{ENV:HTTP_AA}://amit.example.com/content/test/category/6 [R=301,L]