homepage Welcome to WebmasterWorld Guest from 54.161.185.244
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Same htaccess for localhost and remote
same or one htaccess file for localhost and remote (hosting) server
sweetguyzzz

5+ Year Member



 
Msg#: 4538922 posted 12:08 pm on Jan 24, 2013 (gmt 0)

Hello Respected Members and Moderators,

I have solved many of my queries from this great and helpful forum specially with the help of jdMorgan.

Well I have one more issue with htaccess files.

Issue is that when ever I upload htaccess file on my hosting I needs to make changes in htaccess file because there are some coding lines which needs to be changed on hosting and on local server.

My local site location is like D:\wamp\www\example.com\ and I access this site by typing localhost/example.com/

Now htaccess I use on local is below:
DirectoryIndex index.html index.php

Options +FollowSymLinks
RewriteEngine On


#Redirect .com/index.html .php .htm to .com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /example.com/index\.(html?|php)\ HTTP/
RewriteRule ^index\.(html?|php)$ /example.com/ [R=301,L]


# Externally redirect to canonicalize the domain name if a non-canonical
# hostname is requested, in order to prevent duplicate-content problems
#RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
#RewriteRule (.*) http://www.example.com/$1 [R=301,L]


#Rewrite .com/12-category-name/index.html to index.php?category_id=12&category_name=category-name
RewriteRule ^([0-9]+)-([^/.]+)/index\.html$ /example.com/index.php?category_id=$1&category_name=$2 [L]




and now htaccess which I upload on my hosting is below:
DirectoryIndex index.html index.php

Options +FollowSymLinks
RewriteEngine On


#Redirect .com/index.html .php .htm to .com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.(html?|php)\ HTTP/
RewriteRule ^index\.(html?|php)$ http://www.example.com/ [R=301,L]


# Externally redirect to canonicalize the domain name if a non-canonical
# hostname is requested, in order to prevent duplicate-content problems
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]


#Rewrite .com/12-category-name/index.html to index.php?category_id=12&category_name=category-name
RewriteRule ^([0-9]+)-([^/.]+)/index\.html$ http://www.example.com/index.php?category_id=$1&category_name=$2 [L]




Now there are three difference in local and hosting htaccess:
1) In first redirect of localhost, I have to write folder name on right side but in same redirect of hosting I dont have to write folder name, instead have to write website URL (or not required to write there)

2) In second host name redirected to www from non www, so in local host I have to commented these lines because on localhost, server will reutrn localhost in value of HTTP_HOST so it will never match and redirect to actual domain

3) In third rewrite rule, I have to write folder name on right side on localhost. This difference is almost same as 1st one.



Now I have total of two questions to solve my query.
1) How to make a single file for both local and hosting server means what changes can be done in htaccess file that if I ever copy htaccess to hosting, there will be nothing to change in file.

2) In www redirect (second one), I have to write actual domain name there, is there anything else I can do that if I upload same htaccess on any other domain means on multiple domains, there will be nothing to change in htaccess file.



I am waiting for best answers from reputable members like g1smd, lucy24 or may be jd or may be anyone else, sorry if I miss any reputable member name for this Apache part of forum.

Waiting for reply
Thanks in Advance
Sweet!

 

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4538922 posted 12:28 pm on Jan 24, 2013 (gmt 0)

Your third rule is a 302 redirect. Remove the protocol and hostname to change it to a rewrite.

It's not possible to have one file for both localhost and live site and upload the identical file to either place.

You either need two files or some other scheme. I duplicate the rules and comment out the set that do not apply, just before uploading.

# block some specific requests

# block other specific requests

# redirect 1 for localhost
# redirect 1 for live site

# redirect 2 for localhost
# redirect 2 for live site

# internal rewrite

etc.

sweetguyzzz

5+ Year Member



 
Msg#: 4538922 posted 12:46 pm on Jan 24, 2013 (gmt 0)

Your third rule is a 302 redirect. Remove the protocol and hostname to change it to a rewrite.

How to make it 301 redirect? just put flag [R=301] in end of that Rewrite? If just to put flag then sorry to ask that by default each redirect is 302 redirect?

You say to remove protocol and hostname, I understand hostname but what is protocol? sorry but I don't know much about terminologies



I understand that any other scheme required to have same files for both servers but I can't figure out any scheme, I tried much for it but can't success in all redirects because some works but some did not. I tried myself very much. If you can tell any scheme that will be very very good.


I duplicate the rules and comment out the set that do not apply, just before uploading.

You write some redirects and commented rules below this line, I can not get what you want to clear from it so please clear it some.

Waiting for your response.

Thanks
Sweet!

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4538922 posted 2:09 pm on Jan 24, 2013 (gmt 0)

Your last rule on the live site should be

#Rewrite .com/12-category-name/index.html to index.php?category_id=12&category_name=category-name
RewriteRule ^([0-9]+)-([^/.]+)/index\.html$ /index.php?category_id=$1&category_name=$2 [L]


It's a rewrite so remove http:// protocol and the hostname. You do not want a redirect here.

sweetguyzzz

5+ Year Member



 
Msg#: 4538922 posted 2:22 pm on Jan 24, 2013 (gmt 0)

ok, very much thanks about it.

Well I have one idea for .htaccess file coding which will work as my wish, I am creating it and will post here, so can you please verify if it work or not?

Thanks
Sweet

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4538922 posted 2:33 am on Jan 25, 2013 (gmt 0)

I am creating it and will post here, so can you please verify if it work or not?

Yup, that is what makes WebmasterWorld happy. Figure it out for yourself and then we will tell you where you went wrong. Or, ahem, jump up & down and cheer if you got it all right.

If just to put flag then sorry to ask that by default each redirect is 302 redirect?

Right again. The default redirect with [R] flag alone is unfortunately a 302 rather than a 301.

This also applies to redirects done with mod_alias (Redirect by that name). Luckily it does not apply to the Directory-Slash Redirect; that's a 301.

sweetguyzzz

5+ Year Member



 
Msg#: 4538922 posted 11:09 am on Jan 25, 2013 (gmt 0)

Thanks lucy24 for nice response, as you say I have tried one htaccess and send here below.

I am expecting comments about this from you guy and awaiting for it. Please respond asap as I need this to fix urgently


DirectoryIndex index.html index.php

Options +FollowSymLinks
RewriteEngine On


#Redirect .com/index.html .php .htm to .com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ ([/[A-Za-z0-9-]+]*)?/index\.(html?|php)\ HTTP/
RewriteRule ^([a-z0-9-/]*)index\.(html?|php)$ %1/ [R=301,L]


# Externally redirect to canonicalize the domain name if a non-canonical
# hostname is requested, in order to prevent duplicate-content problems
# 1st RewriteCond to check for dot because actual domain always have dot in it and localhost server does not
RewriteCond %{HTTP_HOST} ([\.]+)
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]


#Rewrite .com/12-category-name/index.html to index.php?category_id=12&category_name=category-name
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ (/[A-Za-z0-9-]+)?/([0-9]+)-([^/.]+)/index\.html\ HTTP/
RewriteRule ^([0-9]+)-([^/.]+)/index\.html$ %1/index.php?category_id=$1&category_name=$2 [L]




Still I have confusion on three points, if you guys can clear it.
1) Is third rule ok or it will make any problem?
2) About three bold parts, should I have to escape dot (.) there or not?
3) Is all rules are in best order or any problem with their order?


Please comment about whole and not just above three points, above three points are which I think can be wrong but you guys knows better about more mistakes so kindly check whole. I will be very thankful to you for commenting and pointing mistakes and clearing my above confusions.

Thanks
Sweet!

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4538922 posted 1:23 pm on Jan 25, 2013 (gmt 0)

# 1st RewriteCond to check for dot because actual domain always have dot in it and localhost server does not
RewriteCond %{HTTP_HOST} ([\.]+)

Hey, that's kind of ingenious :)

You don't need the whole ([\.]+) though. First, you don't need the () because you are not capturing. Second, you don't need + because you only need to find one character. (Since there are no anchors, it doesn't matter if there is more than one.) Third, since you are only looking for one specific character you don't need the [] grouping brackets.

If you did need the grouping brackets, you would not need the \ because . inside brackets means a literal dot. But without the brackets you do need the \ and, what do you know, that takes care of your question #2 :)

RewriteCond %{HTTP_HOST} \.

Except you seem to have two [/.] and one [\.] Is that intentional? [/.] is "any character other than a slash or dot". You would often use this in Regular Expressions looking for an URL. But you would never need [\.] with escaped period. Either [.] alone or \. alone.

#Rewrite .com/12-category-name/index.html to index.php?category_id=12&category_name=category-name
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ (/[A-Za-z0-9-]+)?/([0-9]+)-([^/.]+)/index\.html\ HTTP/
RewriteRule ^([0-9]+)-([^/.]+)/index\.html$ %1/index.php?category_id=$1&category_name=$2 [L]


Pay close attention to the parentheses in your Condition. You have three sets:

(/[A-Za-z0-9-]+)?/([0-9]+)-([^/.]+)/index\.html

The second and third sets will not be used for captures, and you don't need them for grouping, so you can leave them out:

(/[A-Za-z0-9-]+)?/[0-9]+-[^/.]+/index\.html

As written, the first set-- the optional one-- is the only one that might be used in the Rule. But this part of the request can never occur, because the Rule itself has an opening anchor. So either get rid of the anchor or put the (/[A-Za-z0-9-]+)? part into the Rule itself.

If you do this, be careful about the directory slashes. In the Condition you have
(/[A-Za-z0-9-]+)?/
but in the Rule this has to be changed to
([A-Za-z0-9-]+/)?

Does the name of this optional directory always start with a letter? If so, say
([A-Za-z][A-Za-z0-9-]+/)?

That way, the RewriteRule will not have to backtrack if the URL only has the second part-- the part that has only numbers before the hyphen. Your target now becomes
$1index.php?category_id=$2&category_name=$3

The back-reference numbers are the same even if one capture is empty.

And now you can go back and simplify the Condition, because you don't need to be so specific about the request. In fact you have to change the Condition-- and also the Rule-- because it will always fail. By the time mod_rewrite reaches this rule, any requests containing "index.html" have already been redirected to the form without index.html.

Come to think of it: What is the index.html doing there at all? This is a brand-new site, right? So you don't have to deal with outdated links or bookmarks. There's no reason for "index.html" to be there in the first place.

sweetguyzzz

5+ Year Member



 
Msg#: 4538922 posted 9:23 pm on Jan 25, 2013 (gmt 0)

Thanks lucy24 for fast and best response. Thanks for good commenting as well.

You given best answer for my second question so all clear about where to escape dot or where not.


Well, you write about many things, I understand many of those but some of those still have some confusions, if you can clear those confusions so basically I will understand the pattern, formula or basis of mod_rewrite so then their will be no more questions instead brain will pull out some more better ideas. So if you don't have any problem then can you please clear those confusions mate.

I have total of 3 confusions right now:

1st Confusion:
As written, the first set-- the optional one-- is the only one that might be used in the Rule. But this part of the request can never occur, because the Rule itself has an opening anchor. So either get rid of the anchor or put the (/[A-Za-z0-9-]+)? part into the Rule itself.

I understand that you say to change here but can't understand why to change (/[A-Za-z0-9-]+)?/ with ([A-Za-z0-9-]+/)?
One terminology you used above is opening anchor, basically I dont know what you mean by anchor so it is problem in understanding this point.

2nd Confusion:
Does the name of this optional directory always start with a letter? If so, say
([A-Za-z][A-Za-z0-9-]+/)?

That way, the RewriteRule will not have to backtrack if the URL only has the second part-- the part that has only numbers before the hyphen.

No, it may be possible that it starts with numbers as well. But you say something here about backtrack, can you please explain it if you wish so?

3rd Confusion:
Your target now becomes
$1index.php?category_id=$2&category_name=$3

The back-reference numbers are the same even if one capture is empty.

I understand changes you told to do in 3rd rewrite rule but the part I bolded in your above quote, why you change that %1 to $1?




Come to think of it: What is the index.html doing there at all? This is a brand-new site, right? So you don't have to deal with outdated links or bookmarks. There's no reason for "index.html" to be there in the first place.

Now about this so you right, site is new brand, no need to handle for outdated links, so I just remove .html from there and only .php remains there because I don't want anyone to access .php which in real exists there because then page with .php extension and page with / (slash in end) both can be seen which will make duplicate content issue, you better know about this mate.



After All:
After seeing above, I have made a big change in 3rd rewrite rule means change that rule to some other thing because that one was only sample, now I am adding what I actually want to use. Please see updated htaccess below:
DirectoryIndex index.html index.php

Options +FollowSymLinks
RewriteEngine On



#Redirect .com/[anything-or-not/]index.html .php .htm to .com/[anything-or-not/]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ ([/[A-Za-z0-9-]+]*)?/index\.php\ HTTP/
RewriteRule ^[a-z0-9-/]*index\.php$ %1/ [R=301,L]


# Externally redirect to canonicalize the domain name if a non-canonical
# hostname is requested, in order to prevent duplicate-content problems
# 1st RewriteCond to check for dot because actual domain always have dot in it and localhost server does not
RewriteCond %{HTTP_HOST} \.
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]


#Rewrite .com/our/contact/ to index.php?type=1&id_name=contact
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([A-Za-z0-9-]+/)?our/[a-zA-Z0-9-]+/\ HTTP/
RewriteRule ^our/([a-zA-Z0-9-]+)/$ /%1index.php?type=1&id_name=$1 [L]



I will be very thankful to you if you answer to the confusions I have and also check with this new htaccess and point mistakes in it and comment about it.

Thanks
Sweet!

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4538922 posted 9:34 pm on Jan 25, 2013 (gmt 0)

Rule 1 can be simplified, but it only works with 0 or 1 folder levels:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([A-Za-z0-9-]+/)?index\.php\ HTTP/
RewriteRule ^[A-Za-z0-9/-]*index\.php$ http://www.example.com/%1 [R=301,L]

If you need multiple folder levels try this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(([A-Za-z0-9-]+/)*)index\.php\ HTTP/
RewriteRule ^[A-Za-z0-9/-]*index\.php$ http://www.example.com/%1 [R=301,L]

I prefer to match and capture in the rule rather than the condition, so actually prefer this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([A-Za-z0-9-]+/)*index\.php\ HTTP/
RewriteRule ^(([A-Za-z0-9-]+/)*)index\.php$ http://www.example.com/$1 [R=301,L]

Slightly faster matching, as you want the rule to quickly check index.php is present:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([A-Za-z0-9-]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://www.example.com/$1 [R=301,L]
The pattern in the condition still enforces the A-Za-z0-9- path requirement.

If that makes you nervous, this also works:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([A-Za-z0-9-]+/)*)index\.php$ http://www.example.com/$1 [R=301,L]

There's always multiple ways to code stuff, but some will be more efficient than others.

In all of the above you should replace A-Za-z with a-z and add the [NC] flag. It parses 33% faster.

sweetguyzzz

5+ Year Member



 
Msg#: 4538922 posted 7:34 am on Jan 26, 2013 (gmt 0)

Sorry to both of you g1smd and lucy24.

I am trying these things on my local and hosting both servers and then checking and responding that why replying with delay.


I understand about NC flag, one thing I want to ask is my order for htaccess ok? Means redirects and rewrites all are in best arrangement?

Waiting for best answer.

Thanks
Sweet!

sweetguyzzz

5+ Year Member



 
Msg#: 4538922 posted 10:16 am on Jan 26, 2013 (gmt 0)

I have two questions, one I posted in above reply and second is:

Is there any way that I process 2 to 3 RewriteRule after checking one RewriteCond means something like below:
RewriteCond %{HTTP_HOST} \.
RewriteRule ^abc1$ abc.php [L]
RewriteRule ^abc2$ abc.html [L]
RewriteRule ^abc3$ abc.htm [L]


For more clearing question, that if HTTP HOST have literal dot in it then those all rewriteRule will work but if find no literal dot in HTTP HOST then no RewriteRule should work?

So can you please guide a way how to do like that?

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4538922 posted 10:59 am on Jan 26, 2013 (gmt 0)

A RewriteCond applies to only the next rule.

If you need it to apply to multiple rules you will need to have the RewriteCond duplicated before each and every rule that it should apply to.



The rule order looks OK. This is the basic plan for any site:

# Rules that block access to malicious requests; i.e. end [F]

# Rules that redirect to new URL; i.e. end [R=301,L]
## Rules that redirect specific requests
## Rules that redirect index requests
## Rules that redirect non-www/www

# Rules that internally rewrite; i.e. end [L]

sweetguyzzz

5+ Year Member



 
Msg#: 4538922 posted 11:30 am on Jan 26, 2013 (gmt 0)

very much thanks for quick response. Now I understand whole htaccess Rules arrangement.

Very much thanks to g1smd and lucy24 for responses and helping me out.

Thanks
Sweet!

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4538922 posted 2:43 pm on Jan 26, 2013 (gmt 0)

I have total of 3 confusions right now:

Why does confusion always sound so much better when it's a non-native speaker of English? Doesn't seem fair somehow...

Just to confuse you even further I am going to answer your last long post, and hope my answer doesn't contradict anything that came later. If there is a disagreement, g1 is right and I am wrong. (Unless there is a typo involving the letter 'k'. By weird coincidence, my own computer is currently eating the letter 'j'. But this is not so serious, except when I am typing Greek and I need that key for breathing marks.)

can't understand why to change (/[A-Za-z0-9-]+)?/ with ([A-Za-z0-9-]+/)?

You are not really changing anything. It's only because the URL format used in THE_REQUEST is slightly different from the URL format used in the body of a RewriteRule. Look at your logs and you will see that the requested URL always begins with a slash / but in a RewriteRule in htaccess there is no slash at the beginning.

Since the capture involves one complete directory, you want to make sure to capture exactly one directory slash so that the target works right. And since there isn't a slash at the beginning in htaccess, you have to capture the one at the end instead.

One terminology you used above is opening anchor, basically I dont know what you mean by anchor so it is problem in understanding this point.

Anchors are an important part of regular expressions. They mean beginning and end:
^ opening anchor = beginning
$ closing anchor = ending
(Yes, both of those symbols ^ and $ also have other meanings in other places. They only do it to confuse you.) If you say

RewriteRule foobar {et cetera}

the rule will work on any URL that contains the text "foobar" anywhere. But if you say

RewriteRule ^foobar

it will only work when "foobar" is the very first thing in the URL. In htaccess, this would mean www.example.com/foobar or www.example.com/foobar/directory/morestuff.html or www.example.com/foobarwidgetsandmore ... et cetera. And if you say

RewriteRule foobar$

it will only work when "foobar" is the very last thing in the URL. Query strings don't count, but extensions do count. So this form would work on www.example.com/directory/otherdirectory/foobar and also on www.example.com/foobar?lots-of-long-complicated-queries but it would not work on www.example.com/foobar.html

No, it may be possible that it starts with numbers as well. But you say something here about backtrack, can you please explain it if you wish so?

Oh, that's too bad. But here is how it works. Your Rule looks as if you can have two types of request: either
www.example.com/blahblah/123-morestuff

or
www.example.com/123-morestuff

In RegEx this is written as
([A-Za-z0-9-]+/)?([0-9]+)-([^/.]+)/

mod_rewrite only works in one direction, reading in a straight line. So when it starts reading a URL, it does not know if it is looking at $1 (the first, optional part) or $2 (the non-optional part) until it gets past the first / slash.

If there is more stuff after the / then everything is fine. The part it has already read is $1 and then it continues to $2 and $3. BUT if there is nothing more after the / then it has to go all the way back to the beginning. This time, it starts matching the pattern for $2 -- the part that is only numbers -- and then the hyphen and then $3.

If $1 always started with a non-number, then your Regular Expression would know right away if it is in* group $1 or group $2.

why you change that %1 to $1

Again, this is because I am shifting part of the pattern from Condition to Rule. When you have (/[A-Za-z0-9-]+)? in the Condition then it is called %1. But if you put it in the Rule then it is called $1.

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ ([/[A-Za-z0-9-]+]*)?/index\.php\ HTTP/
RewriteRule ^[a-z0-9-/]*index\.php$ %1/ [R=301,L]


Hm now this seems to be moving backward. If you have to put something in both the Rule and a Condition, it is always better to capture it in the Rule. And I am very certain that g1 has said something about using anything in the form [a-z0-9-/]* anywhere but at the end of a pattern. Especially when you include directory slashes in the group.

I don't think you need to have that much detail in the Condition. I think it's enough to say
... index\.php\ HTTP
This says that #1 the element "index.php" was part of the original request and #2 the request did not include a query string.


* At this point I had a horrible attack of jamais vu and I am still not sure I got those four words in the right order. It looks like a parody of English as written by a non-Indo-European-speaker:
"In English, and in most other European languages, a sentence is a string of beads. Each bead is a tiny little word, and the beads are strung together to make meaning."

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved