homepage Welcome to WebmasterWorld Guest from 54.198.130.203
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Problem with a slash
Camaleon




msg:3844302
 7:08 am on Feb 7, 2009 (gmt 0)

hi, one of my rules is this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)_([0-9]{1,2})pg([0-9]*)$ example.php?n_cat=$1&idcat=$2&pg=$3 [L]

so, we can have something like this:

example.php/cat_1pg20

the only problem whit this and all my rules is that when i add a slash at the url end i recieve a 404 error

example.php/cat_23pg3/

how can i solve it ?

Thanks alot and sory for my english !

 

Murasaki




msg:3844313
 7:41 am on Feb 7, 2009 (gmt 0)

I've only just started learning mod rewrite a few days ago but I guess I'll give it a go.

If you want both with and without slash to work, try this:
RewriteRule ^([^.]+)_([0-9]{1,2})pg([0-9]*)/?$ example.php?n_cat=$1&idcat=$2&pg=$3 [L]

g1smd




msg:3844395
 12:39 pm on Feb 7, 2009 (gmt 0)

Thanks for diving in and taking a good guess at the solution. It's a little more involved that casual inspection might lead you to believe. :-)
While that code "works", you now have a rewrite such that you have two different URLs that can access the same content. You have just successfully created your first Duplicate Content problem.

Err, actually, it might be the second problem if you aren't already redirecting non-www requests to www.

The solution you actually need is to redirect (not rewrite) those other requests. The code changes are very easy. Drop the slash and question mark back out of the rewrite again. The rewrite will now only respond to a URL request without a slash.

Add a new redirect (using another RewriteRule) before the rewrite. This redirect will force the removal of the slash if one is present, and it will also need to force www at the same time. Use [R=301,L] with it. A similar question was covered in the last few days to get you started.

The effect of the change is this. Users asking for the "wrong" URL will now be redirected to the "right" URL before the content is served from there.

Post your progress back here.

Camaleon




msg:3844643
 8:29 pm on Feb 7, 2009 (gmt 0)

ok, thank you both ! it is working !

so, i followed the g1smd tip and i maked this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)_([0-9]{1,2})pg([0-9]*)$ example.php?n_cat=$1&idcat=$2&pg=$3 [L]
RewriteRule ^([^.]+)_([0-9]{1,2})pg([0-9]*)/?$ http://www.example.com/$1_$2pg$3 [R=301,L]

Add a new redirect (using another RewriteRule) before the rewrite.

but i dont understand why should i put that rule before, if i do that i obtain an error (the website is not redirecting tata in firefox)

Thanks alot !

[edited by: Camaleon at 8:30 pm (utc) on Feb. 7, 2009]

g1smd




msg:3844672
 8:57 pm on Feb 7, 2009 (gmt 0)

The redirect must be listed first as it makes browser terminate the current session and come back with a new request for a new URL.

The redirect takes "incorrect" or "non canonical" URL requests and makes the browser make a new request for the canonical URL.

You need that to happen before the rewrite actually goes and fetches the content "at the new URL".

You also need your links to contain the URL, that you wish users to "see" and "use". Links "define" URLs.

Your redirect contains a question mark, so it takes a URL both with and without a trailing slash and redirects for both conditions. Redirecting back to the same URL would make an infinite loop. Lose the question mark from that rule.

[edited by: g1smd at 9:04 pm (utc) on Feb. 7, 2009]

jdMorgan




msg:3844674
 8:59 pm on Feb 7, 2009 (gmt 0)

Simplifying, covering contingencies, and correcting, I'd suggest:

# Externally redirect direct client requests for dynamic URL to corresponding static URL
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /example.php\?n_cat=[^&]+&idcat=[^&]+&pg=[^\ ]+\ HTTP/
RewriteRule ^example\.php$ http://www.example.com/%1_%2pg%4? [R=301,L]
#
# Externally redirect to remove trailing slash
RewriteRule ^([^_]+_[0-9]{1,2}pg[0-9]+)/$ http://www.example.com/$1 [R=301,L]
#
# Externally redirect to canonicalize the domain name
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Internally rewrite static URL-path requests to dynamic script filepath
RewriteRule ^([^_]+)_([0-9]{1,2})pg([0-9]+)$ example.php?n_cat=$1&idcat=$2&pg=$3 [L]

Completely flush your browser cache(s) before testing any changes to server code.

Jim

Camaleon




msg:3844792
 1:03 am on Feb 8, 2009 (gmt 0)

Hi guys ! thanks alot, the code is working, but i still have a problem whit this rule:

RewriteCond %{REQUEST_URI} !^/(index\.php宅er_categoria\.php字obots\.txt存itemap\.xml奸abels\.rdf安3c/p3p\.xml)$
RewriteCond %{REQUEST_URI} !\.(gif夸pg夸peg?如ng夷co圭ss夸s地vi妃pe?g安av安mv妃p3存wf圩lv)$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)_([0-9]{1,2})pg([0-9]*)$ example.php?n_cat=$1&idcat=$2&pg=$3&id_pro=%2 [L]

Like iam using subdomains i dont know how to complement the jdMorgan rules whis this one to make it work.

[edited by: Camaleon at 1:04 am (utc) on Feb. 8, 2009]

g1smd




msg:3844793
 1:16 am on Feb 8, 2009 (gmt 0)

One correction. Change
jpg夸peg? to just jpe?g
jdMorgan




msg:3844795
 1:31 am on Feb 8, 2009 (gmt 0)

Because your RewriteRule pattern is so specific, it is highly unlikely that you need any of the "qualifier" RewriteConds on that rule. In fact, none of the file exclusions are needed, since files ending in .jpg, etc will not match the pattern. I'd suggest you simply take the rule I posted above, modify it to add the "id_pro" stuff, and use it exactly as shown.

There is no use wasting CPU time checking for RewriteCond patterns that cannot possibly match the rule pattern, and unless you have physically-existing files that will match the RewriteRule pattern, checking the URLs for file- and directory-exists is an enormous waste of time -- mod_rewrite must call the operating system to ask the file manager to go check your disk!

Jim

Camaleon




msg:3844797
 1:42 am on Feb 8, 2009 (gmt 0)

ok, but the id_pro var is on the subdomain, so i dont have idea how to complement that to the rule.

Maybe i have to follow this way ?

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /(www\.)?([^.]+)\.example\.com \?n_cat=[^&]+&idcat=[^&]+&pg=[^&]+&id_pro=[^\ ]+\ HTTP/
RewriteRule ^\.php$ [%2.example.com...] [R=301,L]

iam veryyy lost, sory guys :(

Camaleon




msg:3847754
 7:09 am on Feb 12, 2009 (gmt 0)

Because your RewriteRule pattern is so specific, it is highly unlikely that you need any of the "qualifier" RewriteConds on that rule. In fact, none of the file exclusions are needed, since files ending in .jpg, etc will not match the pattern. I'd suggest you simply take the rule I posted above, modify it to add the "id_pro" stuff, and use it exactly as shown.

There is no use wasting CPU time checking for RewriteCond patterns that cannot possibly match the rule pattern, and unless you have physically-existing files that will match the RewriteRule pattern, checking the URLs for file- and directory-exists is an enormous waste of time -- mod_rewrite must call the operating system to ask the file manager to go check your disk!

Jim

But if i remove the two first lines of that rule, the bold part dosent work:

RewriteCond %{REQUEST_URI} !^/(index\.php宅er_categoria\.php字obots\.txt存itemap\.xml奸abels\.rdf安3c/p3p\.xml)$
RewriteCond %{REQUEST_URI} !\.(gif夸pg夸peg?如ng夷co圭ss夸s地vi妃pe?g安av安mv妃p3存wf圩lv)$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+)_([0-9]{1,2})pg([0-9]*)$ example.php?n_cat=$1&idcat=$2&pg=$3&id_pro=%2 [L]

if i type some.example.com/namecat_1pg2 it just dont works because it is searching files like example.php on /some and that dir will never exist.

This ones are not needed, i can remove it:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

So what should i do ? How can i canonicalize the urls in this case ? i cant get it whit the rule. Thanks alot

[edited by: Camaleon at 7:26 am (utc) on Feb. 12, 2009]

jdMorgan




msg:3847961
 2:33 pm on Feb 12, 2009 (gmt 0)

Logically, the rule should work like this:

RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[b]([^_]+)_[/b]([0-9]{1,2})pg([0-9]+)$ example.php?n_cat=$1&idcat=$2&pg=$3&id_pro=%2 [L]

Notice that the pattern in the RewriteRule only accepts URL-paths ending with "pg<number>". Therefore it is unnecessary to check for URL-paths ending with .php, .txt, .xml, .rdf, .gif, or any other media file extension; None of these end with "pg<number>", and checking for those file extensions is not necessary.

The checks for !-f or !-d are only needed if a requested URL-path such as "/<something>_<number>pg<number>" might actually resolve to a real existing file or directory. If you are never going to name a real physical file "/<something>_<number>pg<number>", then this is never going to happen, and the rule can be further reduced to:

RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteRule ^([^_]+)_([0-9]{1,2})pg([0-9]+)$ example.php?n_cat=$1&idcat=$2&pg=$3&id_pro=%2 [L]

Think about what each line does, and how it applies to your URLs and your requirements; There is no "mysterious magic" involved here, and any problems are likely due to a mis-match between what you have told the server to do (with your mod_rewrite code) and what you actually need the server to do.

It's also possible you are having problems because you are forgetting to always completely flush your browser cache before testing after uploading new code -- This can cause inconsistent and "weird" test results, because the browser will serve "stale" pages and server responses from its cache if the URL you request has been previously visited.

Jim

Camaleon




msg:3848333
 10:11 pm on Feb 12, 2009 (gmt 0)

thanks alot jdMorgan for your desinterested help, you really happy my day ! it is working now:

My htaccess :

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\.example\.com.ar
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com.ar
RewriteRule ^([^.]+)_c([0-9]{1,2})$ script.php?n_cat=$1&id_cat=$2&id_pro=%2 [L]

RewriteCond %{HTTP_HOST} !^www\.example\.com.ar
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com.ar
RewriteRule ^([^.]+)_c([0-9]{1,2})pg([0-9]*)$ script.php?n_cat=$1&id_cat=$2&pg=$3&id_pro=%2 [L]

RewriteRule ^([^.]+)_c([0-9]{1,2})$ script.php?n_cat=$1&id_cat=$2 [L]

RewriteRule ^([^.]+)_c([0-9]{1,2})pg([0-9]*)$ script.php?n_cat=$1&id_cat=$2&pg=$3 [L]

RewriteRule ^([^.]+)_c([0-9]{1,2})/([^.]+)_([0-9]{1,10})$ script.php?n_cat=$1&id_cat=$2&n_anu=$3&id_anu=$4 [L]

RewriteCond %{REQUEST_URI} !^/(index\.php存cript\.php字obots\.txt存itemap\.xml奸abels\.rdf安3c/p3p\.xml)$
RewriteCond %{REQUEST_URI} !\.(gif夸pe?g如ng夷co圭ss夸s地vi妃pe?g安av安mv妃p3存wf圩lv)$
RewriteCond %{HTTP_HOST} !^www\.example\.com.ar
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com.ar
RewriteRule (.*) index.php?id_pro=%2 [L]

But now how can i resolve the trash problem whit the rules that contains the subdomains like 1,2 and 6 lines ?

For example this line:

# Externally redirect to canonicalize the domain name
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

If i type some.example.com it will always redirect to http://www.example.com , so it is some way to tell to that rule that it must distinguish between no-www and a var(some) ?

Thanks alot.

[edited by: Camaleon at 10:14 pm (utc) on Feb. 12, 2009]

jdMorgan




msg:3848491
 3:52 am on Feb 13, 2009 (gmt 0)

That code only works for sites that do not use subdomains, or use only the "www" subdomain. If you use multiple subdomains, then the code is more complicated:

# Externally redirect to canonicalize the main domain name
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Externally redirect to canonicalize the subdomain names
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com\. [NC,OR]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com: [NC]
RewriteRule (.*) http://%1.example.com/$1 [R=301,L]

These rules do basic canonicalization, and also detect and remove FQDN tags and port numbers appended to the domain name. For example, [google.com.:80...] is a perfectly-valid hostname, but non-canonical.

Jim

[edited by: jdMorgan at 1:25 pm (utc) on Feb. 13, 2009]

Camaleon




msg:3850711
 4:22 pm on Feb 16, 2009 (gmt 0)

thanks jdMorgan, a final question:

Using a 303 for the urls whit subdomains i maked this:

RewriteCond %{HTTP_HOST} !^www\.example\.com.ar
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com.ar
RewriteRule ^([^.]+)_c([0-9]{1,2})/$ [%2.example.com.ar...] [R=301,L]
RewriteRule ^([^.]+)_c([0-9]{1,2})$ script.php?n_cat=$1&id_cat=$2&id_pro=%2 [L]

The redirect works well, the problem is that the php script dosent recognise the var that is in the sub-domain.

I mean that if the script redirects some.example.com/dogs_c2/ to some.example.com/dogs_c2 its gonna to recognise only dogs and 2 and it will be the same page as example.com/dogs_c2,any idea how to solve this issue ?

Thanks alot !

jdMorgan




msg:3850719
 4:34 pm on Feb 16, 2009 (gmt 0)

RewriteConds *only* apply to the single RewriteRule that follows them. So %2 in the second rule is undefined.

Duplicate/copy the RewriteConds if you need them for the second rule.

Jim

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