Link to home
Start Free TrialLog in
Avatar of Nura111
Nura111

asked on

can anyone exlian the following RewriteRule in Htaccess file

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

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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".
Avatar of Nura111
Nura111

ASKER

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?
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/
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!
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  ; )
Avatar of Nura111

ASKER

         
    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 ?
Avatar of Nura111

ASKER

Ok sorry ignore my last question! Thank you for the well explained information
Avatar of Nura111

ASKER

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?
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.
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.
Avatar of Nura111

ASKER

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
Do you own the domain name locksmith-daly-city.com? If not, you won't be able to do this.
Avatar of Nura111

ASKER

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?
Yes.
Avatar of Nura111

ASKER

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?
# 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/
Avatar of Nura111

ASKER

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 ?
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.
Avatar of Nura111

ASKER

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