Forum Moderators: phranque
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 !
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.
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]
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]
# 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]
Jim
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]
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
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 :(
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]
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]
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]
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
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]
# 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]
Jim
[edited by: jdMorgan at 1:25 pm (utc) on Feb. 13, 2009]
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 !