开发者

Another mod rewrite question

I have been trying to turn my url in example 1 to example 2 using mod_rewrite but with no success? I'm using godaddy.

Example 1

http://www.example.com/categories/details.php?cat=fruit

Example 2

http://www.example.com/categories/fruit/

Here is my mod rewrite code in my categories folder.

Options +FollowSymLinks
Options -MultiViews
RewriteEngine on
RewriteBase /categories/
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/categories/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php(.*)\ HTTP/ [NC]
RewriteRule ^index\.php(.*)$ http://www.example.com/categories/$1 [R=301,L]

RewriteCond %{QUERY_STRING} ^cat=([a-zA-Z0-9]*)$
RewriteRule ^details\.php$ http://www.example.com/categories/%1.php 开发者_运维问答[R=302,L]


Try this instead.

RewriteCond %{QUERY_STRING} ^cat=([a-zA-Z0-9]+)
RewriteRule ^categories\/details\.php$ http://www.example.com/categories/%1 [R=302,L]

This:

RewriteCond %{QUERY_STRING} ^cat=([a-zA-Z0-9]+)

Should match the query. So it should find cat in the beginning of the query and match the group after cat which is the var. Now you can also match the sub categories too. To match multiple ones you could do something like

RewriteCond %{QUERY_STRING} ^cat=([a-zA-Z0-9]+)\&sub1=([a-zA-Z0-9]+)
RewriteCond %{QUERY_STRING} ^cat=([a-zA-Z0-9]+)\&sub1=([a-zA-Z0-9]+)\&sub2=([a-zA-Z0-9]+)

And then you could use %1,%2,%3 as your variables.

This:

RewriteRule ^categories\/details\.php$ http://www.example.com/categories/%1 [R=302,L]

Will match the entire URL minus the domain. And you use %1 to get the group from the rewritecond.

=============== OLD ===============

I'm not very good with regex, but ^ will match the beginning of a line. So try this out.

^categories\/details\.php\?cat=(.*)$

Then you will need to use http://www.example.com/categories/$1

If you need me to explain the regex to you let me know. This is simple enough so that i can explain it.

you can replace this portion:

RewriteCond %{QUERY_STRING} ^cat=([a-zA-Z0-9]*)$
RewriteRule ^details\.php$ http://www.example.com/categories/%1.php [R=302,L]

with:

RewriteRule ^categories\/details\.php\?cat=(.*)$ http://www.example.com/categories/$1

I can't exactly remember how mod_rewrite works, so i don't want to give you bad information using rewriteCond.

Ok so using what you had before. Try to just add the categories on to it.

RewriteCond %{QUERY_STRING} ^cat=([a-zA-Z0-9]*)$
RewriteRule ^categories\/details\.php$ http://www.example.com/categories/%1 [R=302,L]

Also a note:

I would probably use + instead of * as well. So ^?cat=([a-zA-Z0-9]+)$

Try something like this:

RewriteCond %{QUERY_STRING} ^(.*&)?cat=([a-zA-Z0-9]+)$
RewriteRule ^categories\/details\.php$ http://www.example.com/categories/%2 [L]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜