htaccess redirect dynamic URL to dynamic URL

Posted by Badie, 12-29-2007, 07:51 AM
Hi I have a page that show articles in my site , the url like this www.example.com/show-free.php?n=rest-bios&y=2007 first i remove the the last variable 2007 because no need for it anymore, so the url become like this www.example.com/show-free.php?n=rest-bios and then i make rewrite by htaccess with this code RewriteRule show-free-n-(.*)\.html show-free.php?n=$1 so the pages now show as www.example.com/show-free-n-rest-bios.html the problem is that old URLs are indexd by search engine, so if someone come to the site from search engine , he will access this page www.example.com/show-free.php?n=rest-bios&y=2007 so i want to make redirect 301 from this page www.example.com/show-free.php?n=rest-bios&y=2007 to www.example.com/show-free-n-rest-bios.html how to do that ? and its not only this page , so what i need is how to redirect dynamic URL to other dynamic URL thanks

Posted by foobic, 12-29-2007, 07:24 PM
Try this:

Posted by dmspilot, 12-30-2007, 03:55 AM
What if the query string parameters are in a different order? It won't work right. Actually the OP isn't interested in the year anyway so try: RewriteCond %{QUERY_STRING} n=(.*)&? RewriteRule show-free.php http://www.example.com/show-free-n-%1.html [R=301,L] You could also do it in your show-free.php script. Add a regex at the beginning of show-free.php that checks $_SERVER['REQUEST_URI'] to see if it contains the "old,ugly" url, and if it does, issue a 301 redirect through PHP to the "new,pretty,official" URI. Last edited by dmspilot; 12-30-2007 at 04:09 AM.

Posted by foobic, 12-30-2007, 04:07 AM
Actually, that would cause a loop because if you read the OP he's also redirecting show-free-n-something.php back to show-free.php?n=something. The year in the query string is the only thing identifying an old link.

Posted by dmspilot, 12-30-2007, 04:16 AM
Heh, you are right--it does loop, I apologize. That's not what I expected. I'll look into it more and see if I can get it to work as I imagined it would... Last edited by dmspilot; 12-30-2007 at 04:28 AM.

Posted by Badie, 12-30-2007, 12:38 PM
hi and thanks guys for help i add this to my htaccess still have problem when someone enter try to access the page by this url http://www.example.com/show-free.php?n=C-plus-plus-Variable-Scope&y=2007 its rewrite and redirect it to http://www.lamp4arab.com/show-free-n-introduction&y=2007.html?n=introduction&y=2007 i read much about that , but i did not know how to remove query from the redirected url so i try to change thats give me the right page but the query string still in the end of the url http://www.lamp4arab.com/show-free-n-introduction.html?n=arabic-introduction&y=2007 so should i used php code to remove the rest of query string and then redirect by used header("location:http://www.lamp4arab.com/show-free-n-introduction.html") ? and wont that be bad for SE ? Last edited by Badie; 12-30-2007 at 12:46 PM.

Posted by dmspilot, 12-30-2007, 04:44 PM
So change this line: RewriteRule show-free.php http://www.lexample.com/show-free-n-%1.html [R=301,L] to this: RewriteRule show-free.php http://www.lexample.com/show-free-n-%1.html? [R=301,L]

Posted by foobic, 12-31-2007, 05:24 AM
Looks like it's doing a greedy match - strange, I thought it was non-greedy but it may be version-dependent. You could try this instead: If you do it using a PHP redirect you can still specify a 301, so for SEO it's exactly the same (just a little more resource-intensive).

Posted by Badie, 01-01-2008, 06:47 AM
Thanks now its work good i want to learn about re write and htaccess more, when i back apache doc, i did not understand anything , anyone knows good site that i can learn from it , something start from 0 ? thanks guys and happy new year

Posted by dmspilot, 01-01-2008, 03:55 PM
You could try this: mod_rewrite cheat sheet

Posted by Jay Suds, 01-17-2008, 12:45 AM
I am having a bitch of time making something similar work. We have recently upgraded Kayako v2 to Kayako v3. As a result, the format of dynamic links have changed, as have the ID numbers for articles in our KB. Thus, I want to do redirects like the following: Old URL:http://www.helpdesk.handynetworks.co...ndetails&_i=48 New URL: http://helpdesk.handynetworks.com/su...eid=44&nav=0,7 Note that the article ID changed from 48 to 44 in this instance, and all others. I realize I will have to enter a bazillion rewrite rules to cover all of the articles, but at the very least, I need to get the major ones redirected (DMCA, AUP, TOS) as those old links are in who knows how many places ... So far, I have tried the following, just as a test: rewritecond %{query_string} _a=knowledgebase&_j=questiondetails_i=48 rewriteRule ^esupport/index.php$ http://www.google.com Obviously, I am well aware this will redirect to Google. Unfortunately, the query_string condition isn't matching and I just can't figure out why. Last edited by Jay Suds; 01-17-2008 at 12:49 AM.

Posted by Jay Suds, 01-17-2008, 12:54 AM
And just to add, this is very strange, this rule works: rewriteCond %{query_string} _a=knowledgebase&_j=questiondetails_i=48 rewriteRule ^page.html$ http://www.google.com? [R,L] r http://www.helpdesk.handynetworks.co...ondetails_i=48 this will redirect to google.com but rewritecond %{QUERY_STRING} _a=knowledgebase&_j=questiondetails_i=48 rewriteRule ^esupport/index.php$ http://www.google.com http://www.helpdesk.handynetworks.co...ndetails&_i=48 doesn't. Is the PHP engine screwing with the query string???

Posted by foobic, 01-17-2008, 01:23 AM
I'd guess it's because you're using esupport/file in your RewriteRule. Does the esupport directory exist? Any conflicting rules in .htaccess there? If you're replacing the esupport directory anyway I'd just put your new rules in there as RewriteRule index.php whereeveritsredirectingto.

Posted by Jay Suds, 01-17-2008, 01:25 AM
That esupport directory doesn't exist any more. Strangle, this rule does work ... rewritecond %{QUERY_STRING} ^_a=k(.*)_i=48$ rewriteRule ^esupport/index.php$ /supportsuite/index.php?_m=knowledgebase&_a=viewarticle&kbarticleid=44&nav=0,7 [R=301,L] I don't get it, but I'm glad it's working.

Posted by Jay Suds, 01-17-2008, 01:38 AM
argh. That rule actually seems to be capturing anything with a query string. so for example, now I have rewritecond %{QUERY_STRING} ^_a=k(.*)_i=48$ rewriteRule ^esupport/index.php$ /supportsuite/index.php?_m=knowledgebase&_a=viewarticle&kbarticleid=44&nav=0,7 [R=301,L] rewritecond %{QUERY_STRING} ^_a=k(.*)_i=46$ rewriteRule ^esupport/index.php$ /supportsuite/index.php?_m=knowledgebase&_a=viewarticle&kbarticleid=42&nav=0,7 [R=301,L] And it doesn't matter what i= equals, everything ends up going to the first URL

Posted by dmspilot, 01-17-2008, 01:58 AM
That's not possible. Considering you set a 301 Moved Permanently redirect code your browser probably (should have) cached the redirect the first time you tried it, it's not going to make a new request if you try to visit that link again, so Apache has no opportunity to use the updated RewriteRules. Try clearing your cache or use the "Live HTTP headers" extension for Firefox.

Posted by Jay Suds, 01-17-2008, 02:01 AM
You're right. It was a caching issue.

Помог ли вам данный ответ?

 Распечатать статью

Также читают

Mail the reach the mailbox

Posted by igalst, 05-08-2007, 08:41 AMHello, I have a VPS running Windows 2003 with Plesk 8.1...

Redirecting a subdomain to a folder on the main domain?

Posted by Max Renn, 11-12-2012, 02:46 AMHi all I hope someone can help me with this. Do any...

Shell Script Question

Posted by jscholes, 03-13-2010, 08:55 AMHi, I have a Windows port of the Unix/Linux uptime...

Lots of dedicated server problems.

Posted by erix920, 05-07-2007, 08:26 PMOur current problem is that when we try to start sshd...

Soap

Posted by parag, 03-12-2010, 09:58 AMHello, I installed SOAP - which tells me it has installed...