homepage Welcome to WebmasterWorld Guest from 54.145.172.149
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

    
multiple mod rewrite rules
A few questions
gi sreenivas

10+ Year Member



 
Msg#: 365 posted 5:36 am on Aug 30, 2003 (gmt 0)
i have been searching rules for mod rewrite for the past three days,some how i managed to rewrite some rules.

i am working on rules of
following

1) FIRST RULE

i want to convert follwoing url

http://domain.com/modules.php?name=News&file=subPage&sid=10

as

http://domain.com/catalog/catalogname10.html

RewriteRule ^/catalog/([a-z])([0-9]*).\html$ /modules.php?name=News&file=subPage$sid=$1

2) SECOND RULE
i want to convert follwoing url

http://domain.com/catalog/default.php?cpath=22_23_27

as

http://domain.com/catalog/subcat22_23_27.html

RewriteRule ^/catalog/([a-z])([0-9]+)_([0-9]+)_([0-9])
.\html$ /catalog/default.php?cpath=$1_$2_$3

3) THIRD RULE

i want to convert follwoing url

http://domain.com/catalog/productinfo.php?cpath=22_23_27&productid=36

as

http://domain.com/catalog22_23_27/product36.html

RewriteRule ^/catalog([0-9]+)_([0-9]+)_([0-9])/product([0-9])\.html /catalog/productinfo.php
cpath=$1_$2_$3&productid=$4

my problem is

1)where can i place these rules at httpd.conf or in .htaccess of directories

2)is it possible to write multiple at a times.
how they are executed.any conflict arises?.

3)how to write RewriteCond condtition for above rules

4)is performance of the request-response decreases by using rewriting rules.

thanks
gi_sreenivas

 

jdMorgan

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



 
Msg#: 365 posted 5:40 pm on Aug 30, 2003 (gmt 0)

gi sreenivas,

Welcome to WebmasterWorld [webmasterworld.com]!

1) FIRST RULE
i want to convert follwoing url
[domain.com...]
as
[domain.com...]
RewriteRule ^/catalog/([a-z])([0-9]*).\html$ /modules.php?name=News&file=subPage$sid=$1

RewriteRule ^/catalog/([a-z]+)([0-9]+)\.html$ /modules.php?name=News&file=subPage$sid=$2 [NE]

(The [NE] flag is used to permit you to use the "$sid" string in the substitution - see cited references below.)

2) SECOND RULE
i want to convert follwoing url
[domain.com...]
as
[domain.com...]
RewriteRule ^/catalog/([a-z])([0-9]+)_([0-9]+)_([0-9]).\html$ /catalog/default.php?cpath=$1_$2_$3

RewriteRule ^/catalog/([a-z]+)([0-9]+)\_([0-9]+)\_([0-9]+)\.html$ /catalog/default.php?cpath=$2_$3_$4

3) THIRD RULE
i want to convert follwoing url
[domain.com...]
as
[domain.com...]
RewriteRule ^/catalog([0-9]+)_([0-9]+)_([0-9])/product([0-9])\.html /catalog/productinfo.php?cpath=$1_$2_$3&productid=$4

RewriteRule ^/catalog([0-9]+)\_([0-9]+)\_([0-9]+)/product([0-9]+)\.html /catalog/productinfo.php?cpath=$1_$2_$3&productid=$4

my problem is
1)where can i place these rules at httpd.conf or in .htaccess of directories

As shown above, they will work in httpd.conf. If you wish to use them in .htaccess, you must remove the leading slash from the pattern in the RewriteRule:
RewriteRule ^catalog/([a-z]+)([0-9]+)\.html$ /modules.php?name=News&file=subPage$sid=$2 [NE]

2)is it possible to write multiple at a times.
how they are executed.any conflict arises?.

You can have as many rules as you like. They are processed first-to-last for each http request.
You must prevent conflicts by specifying patterns that precisely match the URIs you wish to rewrite.
You can use the [L] flag to specify that once the pattern matches and a URI is rewritten, the rewrite engine should stop any further Rule processing for the current http request:
RewriteRule ^/catalog/([a-z]+)([0-9]+)\.html$ /modules.php?name=News&file=subPage$sid=$2 [NE,L]

3)how to write RewriteCond condtition for above rules

A RewriteRule may be preceded by any number of RewriteConds. The RewriteConds apply to that rule only, unless you use the Chain flag [C].

This RewriteCond prevents the Rule from being applied if the HTTP_REFERER is your own domain or IP address:
RewriteCond %{HTTP_REFERER} !^(www\.)?mydomain\.com
RewriteCond %{HTTP_REFERER} !^192\.168\.0\.1
RewriteRule ^/catalog/([a-z]+)([0-9]+)\.html$ /modules.php?name=News&file=subPage$sid=$2 [NE]

4) is performance of the request-response decreases by using rewriting rules.
Yes, by a very very tiny amount. mod_rewrite is "built-into" the Apache Server. Therefore, it is much more efficient than any script you might use.

Ref: Introduction to mod_rewrite [webmasterworld.com]
Ref: Apache mod_rewrite documentation [httpd.apache.org]

Jim

gi sreenivas

10+ Year Member



 
Msg#: 365 posted 7:09 am on Sep 1, 2003 (gmt 0)

thanks jim

there is no problem in rewriting url's in our developemt server.
thanks for your information.

when i tried in our remote server it is not working.

is mod_rewrite work with subdomains?

our remote server setup is like this.

httpd.conf file in /etc/httpd/conf/httpd.conf

web content is in /home/virtual/subdomain.domain.com/var/www/html

i can also see httpd.conf in /home/virtual/subdomain.domain.com/etc/httpd/conf/httpd.conf it seem's it is simply mounted the original httpd.conf

mod_rewrite module is there.i also make AllowOverride to All

i put .htaccess file in /home/virtual/subdomain.domain.com/var/www/html

when i access rewrite url

[subdomain.domain.com...]
of
[subdomain.domain.com...]
i am getting forbidden message.

jdMorgan

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



 
Msg#: 365 posted 3:40 pm on Sep 1, 2003 (gmt 0)

What does your error log say?

mod_rewrite requires precision, and therefore, any one of a hundred tiny errors can cause a problem.

You need to use your error log to find out what is wrong.

If insufficient information is present in your error log, you can create a separate log file for mod_rewrite activity, and control the level of logging with the RewriteLog and RewriteLogLevel directives of mod_rewrite [httpd.apache.org].

You might even want to drop back and test a very basic rewrite first:

RewriteEngine on
RewriteRule ^doesnotexist\.html$ /index.html [L]

Put that in .htaccess in the web root directory of a working site with an index page called "index.html" (you cna change the RewriteRule as needed to use the right index page URI). Now, request "doesnotexist.html" from that site. The server should provide the contents of index.html.

If it doesn't work, then you have a problem with the way mod_rewrite is installed or configured on your server.

Jim

gi sreenivas

10+ Year Member



 
Msg#: 365 posted 1:07 pm on Sep 3, 2003 (gmt 0)

hi jim.
rewrite rules are working when i am putting under
<virtual > tags file of httpd.conf. thanks jim

but not with .htaccess.

i have even test with simple .htaccess file you have given,it is not working.

is it compulsory to have Options +FollowSysmlinks?

what actually FollowSymlinks means?

what other options should i need to have in Options directive?

in server only Options +Includes is there?

jdMorgan

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



 
Msg#: 365 posted 4:23 pm on Sep 3, 2003 (gmt 0)

>i have even test with simple .htaccess file you have given,it is not working.

Well then it should be easy to find the problem, since a simple rewrite means fewer possible errors.

>is it compulsory to have Options +FollowSysmlinks?

Yes, either in http.conf or in .htaccess. You will usually get a 500-Server Error if you attempt to use mod_rewrite without it.

>what actually FollowSymlinks means?

It has two meanings. The first is the meaning defined in the Options documentation. The second is that it serves as an "enable" for mod_rewrite use in a per-directory .htaccess context.

I want to point out something that you need to know if you are testing in both http.conf and .htaccess. There is a subtle difference in the required pattern of the RewriteRule.
For .htaccess, use:

RewriteEngine on
RewriteRule ^doesnotexist\.html$ /index.html [L]

For httpd.conf, use:

RewriteEngine on
RewriteRule [b]^/d[/b]oesnotexist\.html$ /index.html [L]

In addition to adding Options +FollowSymLinks, you may also need to use the RewriteBase directive. The best way to figure this out is to examine your server error log after a failed request. The error log will usually show what path resulted from the rewrite, and you can use that to correct the rewrite by adding RewriteBase if needed. For example, you may need to add something like
RewriteBase /var/www/html/ in order to get this working in the top-level web page directory.

Also, since you have access to the server configuration, you can enable detailed rewrite logging using the RewriteLog and RewriteLogLevel directives. Doing so, and reading the rewritelog after each trial, will make it much easier to find problems.

Since every server setup seems to be slightly different depending on the administrator's goals and choices, there is no way I can give you a magic answer for why you are having trouble. You will have to muddle through this, read the Apache documentation, and experiment until you get it working. Once you have it working, you will find it to be extremely powerful and useful. But the initial stages can be difficult and frustrating so I've tried to help, and I encourage you to keep at it until you succeed.

Jim

gi sreenivas

10+ Year Member



 
Msg#: 365 posted 6:45 am on Sep 4, 2003 (gmt 0)

i have given

Options +FollowSymlinks in .htaccess.It is working now.

thanks Jim for your encouragement and support.

i have some more doubts

1)i have an access to httpd.conf,which is the better way regarding performance to put rewrite rules in httpd.conf or .htaccess.

2)i want to know more about RewriteMap directive.At what situations we can use RewriteMap?

jdMorgan

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



 
Msg#: 365 posted 3:17 pm on Sep 4, 2003 (gmt 0)

1) Rewrites in httpd.conf are compiled at server start-up. Rewrites in .htaccess are interpreted for EACH HTTP REQUEST. So, rewrites in httpd.conf are much more efficient, but any change requires a server restart.

2) RewriteMap is useful for many things. Look to RewriteMap when the function you wish to implement involves long lists of URLs with little in common. If you are familiar with using a "table lookup" in any other programming language, you can think of RewriteMap as an analog to that lookup function.

However, RewriteMap can ONLY be used in httpd.conf, and not in .htaccess.

Jim

gi sreenivas

10+ Year Member



 
Msg#: 365 posted 6:05 am on Sep 8, 2003 (gmt 0)

with rewrite rules

i am getting problem in displaying images properly in one of my subdomain's.

RewriteEngine On
RewriteBase /
RewriteRule ^/catalog/StandProducts/$ /modules.php?name=News&file=subPage&sid=55

images are there under images folder in /

modules.php file is processing correclty.

when i have seen RewriteLog file

for images passthrough is taking as /home/virtual/fst4/var/www/html/catalog/

converted rewrite taking as /catakig/StandProducts/images/image1.gif

what is the problem?

is there is any problem in coding?.
it is a php-nuke's code.

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