I am a beginner with htaccess stuff and need help checking for mistakes in my htaccess codes below ?
I have 3 set of redirect codes and wondering if they interfere with each other ?
Also wondering if I have redundant redirect code.
Also specifically within the "Stop Hotlinking Code", is this RewriteCond correct?
RewriteCond %{HTTP_REFERER} !^http://(www\.)%{HTTP_HOST}/.*$ [NC]
My goal is to KEEP the www and trailing slash for my URLs , and to remove the index.html for my URLs
Here is my .htaccess File
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
#Force Trailing Slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1/ [L,R=301]
#Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1/ [R=301,L]
#Redirect index.html
RewriteCond %{THE_REQUEST} ^.*/index.html [NC]
RewriteRule ^(.*)index.html$ http://www.%{HTTP_HOST}/$1/ [R=301,L]
#Stop Hotlinking
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)%{HTTP_HOST}/.*$ [NC]
RewriteRule \.(gif|jpg|jpeg|bmp|zip|rar|mp3|flv|swf|xml|php|png|css|pdf)$ - [F]
.
Edit:
Questions:
-Is my updated htaccess below correct?
-Do I put RewriteBase / at the top or keep it as is (one with the # Begin WordPress rules and a second RewriteBase / with # Force Trailing Slash ?
-Do I need Options +FollowSymLinks and if yes where do I put this?
Notice that I:
-removed the RewriteEngine On and RewriteBase / from WordPress rules and kept it at the top
-put WordPress Rules BELOW redirects
-removed end slashes for #Redirect non-www to www and #Redirect index.html
RewriteEngine On
# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Redirect index.html
RewriteCond %{THE_REQUEST} ^.*/index.html [NC]
RewriteRule ^(.*)index.html$ http://www.domain.com/$1 [R=301,L]
# Force Trailing Slash
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1/ [L,R=301]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
# Stop Hotlinking
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)domain.com/.*$ [NC]
RewriteRule \.(gif|jpg|jpeg|bmp|zip|rar|mp3|flv|swf|xml|php|png|css|pdf)$ - [F]
No it's not correct. You can't have % variables or back references in the regex part of the condition. You could use a regex backreference though. Try:
RewriteCond %{HTTP_HOST}:::%{HTTP_REFERER} !^([^:]+):::http://(www\.)\1/.*$ [NC]
Related
I have the following htaccess file. I do not want to perform a redirect from https to http on a certain file. how can i achieve this?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.spectrumasa.com$ [NC]
RewriteRule ^(.*)$ http://www.spectrumgeo.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^intranet.spectrumasa.com$ [NC]
RewriteRule ^(.*)$ http://www.spectrumgeo.com/ [R=302,L]
RewriteCond %{HTTP_HOST} ^www.asb.com.au$ [NC]
RewriteRule ^(.*)$ http://www.spectrumgeo.com/ [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.spectrum-geopex\.com\.eg$ [NC]
RewriteRule ^(.*)$ http://www.spectrumgeo.com/spectrum-geopex [R=301,L]
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/server-status
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I would like to access only the following through https http://www.spectrumgeo.com/wp-content/uploads/Spectrum_logo_email-171w.jpg
Add a condition to your rule:
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/wp-content/uploads/Spectrum_logo_email-171w\.jpg$
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You can have something complex in picking with to redirect to https:// from http:// but you can accomplish a simple redirect on a single file via this method.
RewriteEngine
Redirect http://www.spectrumgeo.com/wp-content/uploads/Spectrum_logo_email-171w.jpg https://www.spectrumgeo.com/wp-content/uploads/Spectrum_logo_email-171w.jpg
This will solve accessing only the above url you have through https://
If I go to my website at www.xyz.com, the browser returns https://xyz.com. If I go to my website at xyz.com, the browser returns xyz.com. How can I force the browser to show https://xyz.com, when typing in xyz.com? I'm using the following .htaccess code.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.*\.)*xyz.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*\.)*www.xyz.com$ [NC]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://xyz.com/$1 [R]
Try using the [OR] rewrite condition flag (you can also combine the first two):
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} www\.xyz\.com$ [NC]
RewriteRule ^(.*)$ https://xyz.com/$1 [R,L]
Thanks Jon Lin, the blank pages are now working when I use your code with this code.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I'm redirecting an old website to a new URL using RedirectMatch. (I don't need to preserve the original query string, hence the "?" at the end).
RedirectMatch 301 ^/cms/index.php$ http://www.mysite.com?
The redirect works, however I'm left with an ugly question mark at the end URL after being redirected:
www.mysite.com?
Is there any way to remove it?
Edit: I'm throwing in the .htacces file I'm using at the moment:
RedirectMatch 301 ^/cms/index.php$ http://www.mysite.com?
# BEGIN WPSuperCache
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
AddDefaultCharset UTF-8
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{QUERY_STRING} !.*=.*
RewriteCond %{HTTP:Cookie} !^.*(comment_author_|wordpress_logged_in|wp-postpass_).*$
RewriteCond %{HTTP:X-Wap-Profile} !^[a-z0-9\"]+ [NC]
RewriteCond %{HTTP:Profile} !^[a-z0-9\"]+ [NC]
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/$1/index.html.gz -f
RewriteRule ^(.*) "/wp-content/cache/supercache/%{HTTP_HOST}/$1/index.html.gz" [L]
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{QUERY_STRING} !.*=.*
RewriteCond %{HTTP:Cookie} !^.*(comment_author_|wordpress_logged_in|wp-postpass_).*$
RewriteCond %{HTTP:X-Wap-Profile} !^[a-z0-9\"]+ [NC]
RewriteCond %{HTTP:Profile} !^[a-z0-9\"]+ [NC]
RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{HTTP_HOST}/$1/index.html -f
RewriteRule ^(.*) "/wp-content/cache/supercache/%{HTTP_HOST}/$1/index.html" [L]
</IfModule>
# END WPSuperCache
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule ^(.*)$ "http\:\/\/www\.mysite\.com\/$1" [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
EDIT: In the end I got it working using anubhava suggestion. BUT I had to make a separate .htaccess file in the /cms folder.
Better use mod_rewrite using code like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^cms/index\.php$ http://www.mysite.com/? [L,R=301,NC]
i have 2 domains:
- domain1.org
- domain2.org
i have installed wordpress in http://domain1.org/wordpress and i need that users enter with http://domain2.org
Currently my htaccess is(based on other posts readed here):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?domain2\.org$ [NC]
RewriteRule ^(.*)$ http://www.domain1.org/wordpress/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
</IfModule>
this works but always show in the links the domain1 url:
http://domain1.org/wordpress/category/furniture/
and in the browser bar i have http://www.domain2.org
Is the htaccess wrong? i have domain2 with godaddy and i'm doing a masked forward.Maybe i should park it?
thk.
# BEGIN WordPress
<IfModule mod_rewrite.c>
# tell mod_rewrite to activate and give base for relative paths
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?PARKED.com$
RewriteCond %{REQUEST_URI} !^/PARKED/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /PARKED/$1
RewriteCond %{HTTP_HOST} ^(www.)?PARKED.com$
RewriteRule ^(/)?$ PARKED/index.php [L]
# for main domain
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule . index.php [L]
</IfModule>
# END WordPress
I have an issue that's too complex for me to handle, but I'm betting someone has had to do this before, so please let me hear from you. ;)
Here's the situation:
I've got 1 main domain with 3 subdirectories that are nested within each other
(from top to bottom)
http://main-domain.com
then
http://main-domain.com/company-name/
then
http://main-domain.com/company-name/blog/
There's currently 3 .htaccess files -- 1 in each of the 3 directories shown above.
What's the problem?
Instead of having www.main-domain.com/company-name/blog/whatever, I'd like to have main-domain.com/blog/whatever
So, I want to drop the www AND more importantly, drop the middle subdirectory; i.e. /company-name/
I hope that the following examples will help to illustrate the point.
http://main-domain.com/company-name/index.php should be changed to http://main-domain.com/index.php
http://main-domain.com/company-name/blog/my-first-article/ should be changed to http://main-domain.com/blog/my-first-article/
Why do I need this?
I need a shorter URL that is more SEO-friendly. I have too many backlinks that use the 'old' urls, so I need to mod-rewrite them all.
Here are My Current 3 htaccess files
root htaccess: main-domain.com
#Bypass InoCore Templating System
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /reservations/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /reservations/default.php [L]
Options -Indexes
</IfModule>
#END Bypass
#301 REDIRECT
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^info.php - [L]
RewriteCond %{HTTP_HOST} ^www.domain1.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^domain1.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^www.domain2.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^domain2.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^www.domain3.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^domain3.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^main-domain.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^www.main-domain.com [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/company-name/$1 [R=301,NC]
company-name htaccess: main-domain.com/company-name/
RewriteEngine on
RewriteRule ^maping.php /maping.php
RewriteRule ^$ index.php?$1 [L]
RewriteRule (.*) index.php?$1 [L]
#php_flag magic_quotes_gpc off
#BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /company-name/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /company-name/index.php [L]
</IfModule>
#END WordPress
blog htaccess: main-domain.com/company-name/blog/
RewriteEngine off
#BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /company-name/blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /company-name/blog/index.php [L]
</IfModule>
#END WordPress
Your correct and compact root .htaccess should be like this:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^info.php - [L]
# match all the domains in single condition while www. is optional
RewriteCond %{HTTP_HOST} ^(www\.)?(domain1|domain2|domain3|main-domain)\.(com|tld)$ [NC]
RewriteRule ^company-name/(.*)$ http://www.domain.tld/$1 [R=301,L,NC,NE]
R=301 will redirect with https status 301
L will make last rule
NE is for no escaping query string
NC is for ignore case comparison
$1 is your REQUEST_URI matching group