remove www from web address - web

I'm running php on IIS 8.0.
when I enter the site address with www server returns IIS Error 500. with out www everything is OK.
How can I automatically remove www from adress.

you can use url rewrite module
add this code to web.config and replace domain.tld with your domain name
<rule name="Remove www" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" />
</conditions>
<action type="Redirect" url="http://domain.tld{PATH_INFO}" />
</rule>

There are multiple ways to redirect your site to domain only,
1) using DNS server, in DNS you can redirect your visitors to domain only,
2) using IIS config file -> C:\Windows\System32\inetsrv\config.

You don't want to simply redirect because you will have to do that for every page and subdomain. My code will auto strip the www. for you.
.htaccess code
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
If you want to fix you DNS issue just add a 'A' record with www.yourwebsite.com

Related

IIS RewriteBase for an .htaccess

In IIS, RewriteBase is not working. So the below .htaccess rule has been converted as an IIS web config. But in IIS it is giving 400 Bad Request error. What do I need to correct?
Htaccess Rule
RewriteRule api/(.*)$ api.php?request=$1 [QSA,NC,L]
IIS Converted Rule
<rule name="Imported Rule 1" stopProcessing="true">
<match url="api/(.*)$"/>
<action type="Rewrite" url="api.php?request={R:1}" appendQueryString="true"/>
</rule>
Assuming your RewriteBase directive is set like this:
RewriteBase /subdir
Then you simply need to append this "prefix" to the start of the target URL (ie. RewriteRule substitution or url attribute value in the IIS directive). Specifically, it should be prefixed only to relative substitution strings.
For example:
RewriteRule api/(.*)$ /subdir/api.php?request=$1 [QSA,NC,L]
In other words:
<action type="Rewrite" url="/subdir/api.php?request={R:1}" appendQueryString="true"/>
If RewriteBase / is set then just prefix with a single slash, eg. url="/api.php?request={R:1}"
That's all that RewriteBase directive does. It sets the URL-prefix for relative path substitutions when used in a directory context.

HTTPS redirect in .htaccess is causing infinite redirect loop

I'm trying to redirect all HTTP urls to HTTPS using the following in .htaccess:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
However it just causes an infinite redirect loop. There is a valid SSL certificate and the website is written in asp.net in case it matters. Google chrome automatically redirects to https, but firefox and edge do not so I need to add this rule. The only way I can get the site to load in https is if I explicitly type https:// in the address. What's the possible cause here?
IIS does not support .htaccess file.so I suggest you use iis URL rewrite module and add a rule using that.
for that, you need to install the URL rewrite module by using the web platform installer or from this below link.
https://www.iis.net/downloads/microsoft/url-rewrite
than add below code in your config file:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="HTTP to HTTPS" enabled="true" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="off" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent"/>
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
for more detail you could refer this below link:
https://stackoverflow.com/a/55531655/11147346
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://yourwebsite.com/$1 [R,L]
try that
or use cloudflare to cache and perfomace your project
https://support.cloudflare.com/hc/en-us/articles/115000219871-Troubleshooting-redirect-loop-errors
https://community.cloudflare.com/t/cloudflare-flexible-ssl-is-it-worth-it/28043
https://developers.cloudflare.com/ssl/origin/ssl-modes/

asp htaccess 301 redirect

I'm trying to redirect 3 pages but for some reason, it doens't seem to work. Site is built with asp
<IfModule mod_rewrite.c>
# Make sure directory listing is disabled
Options +FollowSymLinks -Indexes
RewriteEngine on
# Re-write for PDFs requests not pre-pended with /pdf/ I.E. in the root - prepend /pdf/ - Use negative look-ahead - If the file doesn't exist you get a 500 though sadly.
RewriteCond %{REQUEST_URI} ^/(?!pdf/)(.*)\.(pdf)$
RewriteCond %{DOCUMENT_ROOT}/pdf/$1 -f
RewriteRule ^(.*)$ /pdf/$1 [L]
# If the URI is not in /images,/pdf,/css, or /js let the handler process it
RewriteCond %{REQUEST_URI} !^(/images/|/pdf/|/css/|/js/)
RewriteCond %{REQUEST_FILENAME} !handler.php
RewriteRule ^(.*)$ handler.php/$1 [L]
# If the URI IS in the above directories but the file doesn't exist run it through the handler as well
RewriteCond %{REQUEST_URI} ^(/images/|/pdf/|/css/|/js/)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ handler.php/$1 [L]
# Permanent URL redirect
Redirect 301 /Server_Rack_Cabinet_RS_42U.asp http://www.rackmountsolutions.net/cruxial-core-1000-series-server-rack.asp
Redirect 301 /Sound_Dampening_Proof_Server_Rack.asp http://www.rackmountsolutions.net/AcoustiQuiet_Soundproof_Server_Rack.asp
Redirect 301 /Relay_Rack_4_post.asp http://www.rackmountsolutions.net/4_Post_Server_Rack.asp
</IfModule>
The # Permanent URL redirect portion doesn't seem to work. The above is the entire code I have in the .htaccess file.
Since this site is running on an IIS server, there was a web.config file overruling the .htaccess file somehow. See the to see how i did it. The web.config file was in my site's root folder.
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Env-leader-redirect1" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^Server_Rack_Cabinet_RS_42U.asp" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.rackmountsolutions\.net$" />
</conditions>
<action type="Redirect" url="http://www.rackmountsolutions.net/cruxial-core-1000-series-server-rack.asp" />
</rule>
<rule name="Env-leader-redirect2" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^Server_Rack_Cabinet_RS_42U.asp" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.rackmountsolutions\.net$" />
</conditions>
<action type="Redirect" url="http://www.rackmountsolutions.net/cruxial-core-1000-series-server-rack.asp" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

How to rewrite htaccess RewriteCond http_cookie into web.config alternative?

I'm having a hard time rewriting the following RewriteCond into web.config alternative and would appreciate any help.
RewriteCond %{HTTP_COOKIE} !^.*wordpress_logged_in_.*$
Using URL Rewrite module in IIS 7, rewrite rules with conditions would look something like this:
<rules>
<rule name="something">
<match ... />
<conditions>
<add input="{HTTP_COOKIE}" pattern="^.*wordpress_logged_in_.*$" />
</conditions>
<action ... />
</rule>
</rules>
You can also use the URL Rewrite GUI interface in a local IIS 7 install to help build your rewrite rules, and then open the corresponding web.config to see how it should look.

Converting web.config Url rewrite to .htaccess

I have switched from a Windows Server 2008 R2 running IIS7.5 to CentOS server running Apache2, as I had performance problems with PHP.
My main site's Web.config uses url rewrite and needs to be converted. It has been a while since I last used .htaccess files.
My Web.Config rewrite code:
<rule name="IndexRewrite" stopProcessing="true">
<match url="^([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="index.php?p={R:1}" />
</rule>
So what it does is Rewriting the ?p= That is used by PHP to display the appropriate page.
So, how exactly can this be done? I am not familiar with mod_rewrite in Apache2.
I tried to modify a rewrite rule from another site using SocialEngine, nut no luck.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
# Get rid of index.php
RewriteCond %{REQUEST_URI} /index\.php
RewriteRule (.*) index.php?p= [L,QSA]
# Rewrite all directory-looking urls
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*) index.php?p= [L,QSA]
</IfModule>
Example of the link:
http://example.com?p=about
Should be
http://example.com/about
That's how it was before when I used IIS7.5 Url rewrite.
Any help?
I have successfully converted it to a working .htaccess mod_rewrite code.
It seems easier than it is. I had to search deeper in Google and found a working toturial.
Here's the code that I use now.
RewriteEngine On
RewriteBase /
RewriteRule ^([A-z]+)$ /index.php?p=$1
So this is solved. ;)

Resources