Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

can anyone exlian the following RewriteRule in Htaccess file

Posted on 2011-10-18
22
Medium Priority
?
557 Views
Last Modified: 2013-11-19
Can anyone exlian the following RewriteRule in Htaccess file
I dont really know a lot about htaccess directive I do know that's changing the url but im trying to get a fully understanding of how its does that where exactly is it talking the  wildcard from ([a-z0-9]*)
Thank you!
RewriteRule ^([_a-z0-9]*)-locksmith-([-a-z0-9]*)\.html$		/index.php?city=$1&market=locksmith&state=$2 [NC,L]

Open in new window

0
Comment
Question by:Nura111
  • 10
  • 10
  • 2
22 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 36989870
See if this makes sense:

RewriteRule ^([_a-z0-9]*)-locksmith-([-a-z0-9]*)\.html$            /index.php?city=$1&market=locksmith&state=$2 [NC,L]
##############################################################################################################################################
##                                                               Search                                                                     ##
##############################################################################################################################################
^            :  Start of string
( ... )      :  Capture group. Save the stuff matched by the pattern inside parens for later use (this is capture group 1)
[_a-z0-9]*   :  Zero or more ( * ) of any character that is an underscore, an alphabetic character (lowercase only), or a digit ( [_a-z0-9] )
-locksmith-  :  Literal text. Find hyphen, "locksmith", hyphen
( ... )      :  Capture group. Save the stuff matched by the pattern inside parens for later use (this is capture group 2)
[_a-z0-9]*   :  Zero or more ( * ) of any character that is an underscore, an alphabetic character (lowercase only), or a digit ( [_a-z0-9] )
\.           :  Literal period/dot
html         :  Literal text. Find "html"
$            :  End of string


##############################################################################################################################################
##                                                               Rewrite                                                                    ##
##############################################################################################################################################
/index.php?city=          :  Literal text.
$1                        :  Regex backreference. Refers back to a capture group in "Search". In this case, capture group 1
&market=locksmith&state=  :  Literal text.
$2                        :  Regex backreference. Refers back to a capture group in "Search". In this case, capture group 2

##############################################################################################################################################
##                                                               Flags                                                                      ##
##############################################################################################################################################
NC           :  Make search case-insensitive ([a-z] matches "a" or "A")
L            :  "Last" - Don't process any other RewriteRule's that follow this one

Open in new window

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 2000 total points
ID: 36989875
P.S.

The flag "NC" stands for "no case" (i.e. case-insensitive).
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36989891
As an example, let's say you have the following URL:

http://www.example.com/123-locksmith-456.html

Open in new window


This is what the rewrite would find in search:

^        ([_a-z0-9]*)        -locksmith-        ([-a-z0-9]*)        \.html        $
             123             -locksmith-            456              .html        

Open in new window


and the replacement would be:

http://www.example.com/index.php?city=123&market=locksmith&state=456

Open in new window


because capture group 1 captured the "123" and capture group 2 captured the "456".
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

Author Comment

by:Nura111
ID: 36989918
OK two issues:
a. in the result test its seem like its doing the oposite replace the php in .html
you can look in http://www.locksmith-dalycity.com/ to see what I mean

b.<<This is what the rewrite would find in search:
Where would it find it I read about it a little bit and understood that:
Apache scans all incoming URL requests, checks for matches in our .htaccess file and rewrites those matching URLs to whatever we specify

where would it search it for?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36990129
in the result test its seem like its doing the oposite replace the php in .html
To be clear, RewriteRule doesn't rewrite the URL that your users see; it rewrites the URL that your server sees when someone clicks on a link. In my previous example, your user would see http://www.example.com/123-locksmith-456.html, but the server would rewrite this URL and afterward it would appear like the user clicked on a link that said http://www.example.com/index.php?city=123&market=locksmith&state=456. You typically use this behavior to provide "pretty" links in your page (http://www.example.com/products/productid/1) and have the server process these links in a more familiar query string format (http://www.example.com/index.php?productid=1).

where would it search it for?
For my example above, it would search after the http://www.example.com/
0
 
LVL 10

Expert Comment

by:Derokorian
ID: 36993321
Side note:

>>To be clear, RewriteRule doesn't rewrite the URL that your users see;

Unless you use the R flag (redirect), in which case the user WOULD see a different URL!
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36993405
in which case the user WOULD see a different URL!
I am referring to the output as seen in the web page (i.e. all of the <a> tags). In order to get this functionality (rewriting outgoing links), you would have to add another utility like mod_seo from Helicon.

A redirect is not a rewrite...  it's a redirect  ; )
0
 

Author Comment

by:Nura111
ID: 36994125
         
    where would it search it for?

<<For my example above, it would search after the http://www.example.com/
maybe its not ht access anymore but what control on the URL on the screen whats create it in the application ?
0
 

Author Comment

by:Nura111
ID: 36994377
Ok sorry ignore my last question! Thank you for the well explained information
0
 

Author Comment

by:Nura111
ID: 36994521
Ok another issue though:
you said  RewriteRule doesn't rewrite the URL that your users see

in the same website :there is other rewrite in the htaccess
a similar rewrite for the one you explained me about is:
RewriteRule ^contact-([a-z0-9]*)-([a-z0-9]*)\.html$            /contact2.php [NC,L]

SO I understand what it does  but there is also
RewriteRule ^contact\.php$                              /contact-dalycity-locksmith.html [NC,R=301]

so that's actually with the R flag as you explained id  actually redirect it
but how come i need to use if its already  an html?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36994760
That rule looks for someone trying to access the page "contact.php" on your site. If someone requests that specific page, then they are redirected to "contact-dalycity-locksmith.html". The address the user sees in the address bar will reflect the new address--the HTML page.
0
 
LVL 10

Expert Comment

by:Derokorian
ID: 36994810
the 301 also tells search engines that the page no longer exists at this address but instead has been moved to the new address given.
0
 

Author Comment

by:Nura111
ID: 36994824
you mean by typing into the url domain.com/contact.php ?

ok I have a small issue I can open a new question for it if you like
Im trying to change my url for indtead of dalycity to daly-city

I thought I can do it by changing the following lines in the htaccess:


# force www
RewriteCond %{HTTP_HOST}      ^locksmith-dalycity\.com
RewriteRule ^(.*)$            http://www.locksmith-dalycity.com/$1 [NC,R=301]

to

RewriteCond %{HTTP_HOST}      ^locksmith-daly-city\.com
RewriteRule ^(.*)$            http://www.locksmith-daly-city.com/$1 [NC,R=301]


but its doesnt seem to work
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36994903
Do you own the domain name locksmith-daly-city.com? If not, you won't be able to do this.
0
 

Author Comment

by:Nura111
ID: 36994931
Oh right :)
but I still will be able to change the pages name e.g
http://www.locksmith-dalycity.com/contact-dalycity-locksmith.html
to contact-daly-city-locksmith.html
right?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36994942
Yes.
0
 

Author Comment

by:Nura111
ID: 36995886
Can you explain two more rewrite rules please? I can open a new question and link to it if you would like.

first -the one we talked about before:

# force www
RewriteCond %{HTTP_HOST}      ^locksmith-dalycity\.com
RewriteRule ^(.*)$            http://www.locksmith-dalycity.com/$1 [NC,R=301

second:
RewriteCond %{QUERY_STRING}                              ^locksmith=([a-z]*)$
RewriteRule ^$                                          /index.php?domain_id=3j8ks&arealink=%1&redirect=1 [NC,L]


Thank you. do you have a good reference to learn about this area how would you say its call is it server adminstration?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36995990
# force www
RewriteCond %{HTTP_HOST}      ^locksmith-dalycity\.com
RewriteRule ^(.*)$            http://www.locksmith-dalycity.com/$1 [NC,R=301
This doesn't permit a user to go to "http://locksmit-dalycity.com". They must use the "http://www.locksmith-dalycity.com". The RewriteCond checks the value of the domain name the user entered; if it did not include the "www", then the RewriteRule redirects the user to the "www" page.

Let me clarify one thing:  I said "doesn't permit," but in actuality, the RewriteRule isn't restricting access. Rather it is seeing that the user requested the version that does not include "www" and it is redirecting the user to the version that uses "www".

second:
RewriteCond %{QUERY_STRING}                              ^locksmith=([a-z]*)$
RewriteRule ^$                                          /index.php?domain_id=3j8ks&arealink=%1&redirect=1 [NC,L]
I don't believe this one works, because the RewriteCond checks that there is a querystring parameter called "locksmith" and it has a value that consists of zero or more ( * ) alphabetic characters ( [a-z] ). However, the RewriteRule tries to match an empty resource request (i.e. no page; no querystring). I might be mistaken.

Assuming it does work for you, then it should redirect any request with a querystring parameter of locksmith and the appropriate value to your "index.php" page, passing the querystring parameters, "domain_id", "arealink", and "redirect". The respective values of those querystring parameters are, "3j8ks", the alphabetic characters matched by the RewriteCond (because the pattern uses capturing parentheses), and "1".

do you have a good reference to learn about this area
For mod_rewrite:  http://httpd.apache.org/docs/current/mod/mod_rewrite.html
For regular expressions:  http://www.regular-expressions.info/
0
 

Author Comment

by:Nura111
ID: 36996135
the second- so this 2 lines are going together right?

arealink=%1 - its going to match here the whole thing? (locksmith=([a-z]*)$)

does it mean that I will necessarily have to see this parameter query string in the URL ((locksmith=([a-z]*)$)
or its can be happening behind the scenes meaning just to the server ?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36996181
arealink=%1 - its going to match here the whole thing? (locksmith=([a-z]*)$)
The %1 syntax works similar to the $1 syntax we covered in the first few posts, except that it refers to something captured in the RewriteCond, not the RewriteRule. In a crude way:

    RewriteCond: %1
    RewriteReule: $1

Both syntaxes refer to the data that was captured by the first capture group; the difference in syntax indicates from where the data was found.

does it mean that I will necessarily have to see this parameter query string in the URL ((locksmith=([a-z]*)$)
You don't have to see it in the URL, but if it's not there then that RewriteRule won't be executed because the preceding RewriteCond will not be true. The RewriteCond expects to see that string in the URL, and if it doesn't see it, then the RewriteRule is never executed.
0
 

Author Comment

by:Nura111
ID: 36996193
o because I know for a fact that it is doing something in the website but I cant find any url like that while browsing the website
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
No other job is as rewarding and demanding as building an iPhone app is. It is not really in the hands of the developer for the success of an iPhone app. Many factors operate jointly for every iOS application's success in the market.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month14 days, 8 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question