Which redirection needed for this in .htaccess? 301 and?

Which redirection needed for this in .htaccess? 301 and? - .htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, 301-redirect, , , .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :


I have two cases, it is very hard for me to separate them. I see them as redirection -problem.



# 1.    I want to redirect all .*/feeds to mySite.com/feeds showing
# the content of mySite.com/Layout/feeds.html but keeping the
# url intact as mySite.com/feeds
# 2. I want to redirect all non-existent art.mySite.com/.* to
# art.mySite.com
RewriteEngine On

#REDIRECT pages that are not specifically redirected
#
# Alert! I am not sure whether this intervenes, sites not
# of that Regex -match are directed to the parametered url.
#
RewriteCond %HTTP_HOST !^www.mySite.com$ [NC]
RewriteRule ^(.*)$ http://www.mySite.com/$1 [R=301,L]


Case 1



#1. BROKEN: it directs mySite.com/feeds to mySite.com according to the 
# above notice, apparently?!
#
# mySite.com/feeds and feeds.mySite.com must show the
# content on mh.com/Layouts/feeds.html but the url must remain
RewriteCond %HTTP_HOST ^www.mySite.com/feeds$ [NC]
RewriteRule ^(.*)$ http://www.mySite.com/Layouts/feeds.html [R=301,L]
RewriteCond %HTTP_HOST ^feeds.mySite.com/$1 [NC]
RewriteRule ^(.*)$ http://www.mySite.com/Layouts/feeds.html [R=301,L]


Case 2



#2. BROKEN
# art.mySite.com/feeds directs to Blogger -site...BAD. It is because
# art.mySite.com displays the Blogger content but it tries do display
# Blogger content also in non-blogger urls such as art.mySite.com/aoeu
#
# not doing anything or?
#RewriteCond %HTTP_HOST ^art.mySite.com/.*$ [NC]
#RewriteRule ^(.*)$ http://friend.blogspot.com/$1 [R]

# WORKS i.e. directs the art.mySite.com to the Blogger and keeps the domain
# but this does not handle the non-existing cases trying to do with (2)
RewriteCond %HTTP_HOST ^art.mySite.com
RewriteRule ^(.*)$ http://friend.blogspot.com/$1 [R]

Solution :

Remember that the order of rewrite rules is very important as rule processing stops once a rule is matched.



For example, if you first rewrite everything that is not http://www.mysite.com, then you will never match http://something.mysite.com later on because the host has already been rewritten. In this case, you would want to rewrite your feed url's first, and rewrite your traffic to http://www.mysite.com in a rule later on.



Rewrite rules can be extremely tedious and cryptic and will just take some time to figure out (it is well worth the effort!).



To start, enable rewrite logging so you can see what is happening:



RewriteEngine On
RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 9


RewriteLogLevel is a 0-9 value, with 9 being debug. Be sure to turn this off on production:



### I just comment them out when not in use ###
#RewriteEngine On
#RewriteLog "/var/log/apache2/rewrite.log"
#RewriteLogLevel 9


You will need to reload your apache once you have enabled or disabled logging: /etc/init.d/apache2 reload for example.



I then usually open a new terminal and tail -f /var/log/apache2/rewrite.log while I am debugging.



Of course, never play with rewrites on a production system as it is extremely easy to bring things to a halt ;)



Hope this helps!


Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.

Comments

Popular posts from this blog

Rewrite in Mediawiki, remove index.php, .htaccess

.htaccess rewrite wildcard folder paths from host

Using .htaccess to set a cookie and 301 redirect