Forum Moderators: phranque

Message Too Old, No Replies

Redirect 301 from dynamic page to static page

         

Alejandro

1:37 am on Jan 6, 2012 (gmt 0)

10+ Year Member



Hi .. I have a problem and I want to do a 301 redirect a dynamic page to a static url. (There are thousands of dynamic pages)

My site is based on PHP and is an image hosting .. so each image has multiple names

I have this type of URL: http://example.com/view.php?filename=827barca.jpg (image)

and I put it like this: http://example.com/viewimages/827barca.jpg

I put this code in the htaccess:

RewriteRule ^ view.php? Filename = $ 1 (.*)$ ViewImage /? [L, R = 301]

But I think it does not work, I hope I can help.

g1smd

1:51 am on Jan 6, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Check previous threads as this is a qustion that is asked at least once per week.

The code you need depends on whether the internal server path is still dynamic or not. If it is you'll need both a redirect and a rewrite.

You'll certainly need a preceding RewriteCond looking at either QUERY_STRING or THE_REQUEST for the redirecting RewriteRule.

The RewriteRule pattern sees only the path part of the request not the host name or query string.

The redirect target should also include the correct protocol and hostname.

The redirect might be only half of the puzzle. It tells users asking for one URL to instead ask for another. You might also need an internal rewrite to connect the new URL requests with the actual internal filepath where the content really resides.

lucy24

1:59 am on Jan 6, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It's been a couple of weeks. Let's paste in that boilerplate again ;)

Query Strings

The Query String, also known as a Parameter, is the part of an url after the question mark. Question = query.

By default, rewrites simply ignore the query string. That is, mod_rewrite stashes the query in a safe place, does its stuff to the part before the question mark, and then reappends the original query.

Changing a Query

#1 To delete a query, add a ? to the end of your rewrite target.
#2 To replace a query—or create a new one—add ?blahblah to the rewrite target. The blahblah can be either literal text, or stuff you captured earlier. (#1 and #2 are really the same thing: you're just replacing the query with either something or nothing.)
#3 To add to an existing query, again put ?blahblah at the end of the target, but also add [QSA] to your flags (the bracketed items at the end of the Rule). It stands for "Query String Append", meaning that the blahblah is to be added to the existing query—if any—instead of replacing it.

Getting the Query

You only need to retrieve the original query if
#1 you want the rewrite to behave differently depending on what the query was
or
#2 you need to change or delete the query

Add a Condition that says

RewriteCond %{QUERY_STRING} blahblah


using your ordinary Regular Expressions, anchors and ! as needed.

To test whether there was a query at all

RewriteCond %{QUERY_STRING} .


which simply means "If the query contains at least one character of any kind".

If you need to capture any of the query, use parentheses as usual. In the rewrite target, the captures will be %1, %2 etc instead of $1, $2 etc, because they are coming from a Condition instead of the Rule. Each set is separately numbered, so the first capture from the Rule will still be $1.

Alejandro

2:08 am on Jan 6, 2012 (gmt 0)

10+ Year Member



I just want to make a 301 redirect to:

1st: make the url more friendly

2nd: that Google detects duplicate content I

and as a script to upload images you generate many links .. multiple pages are dynamic. I just want to put the static page for the reasons mentioned above.

The code would look like more or less?

g1smd

2:18 am on Jan 6, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Let's see your code. There are two thousand previous answers on this topic to draw from: [google.com...] (webmasterworld)

Alejandro

2:25 am on Jan 6, 2012 (gmt 0)

10+ Year Member



I have seen several, but the code includes the id of the dynamic url and my case is no id ..

Example: "view.php?filename=(multiple id)"

g1smd

3:08 am on Jan 6, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Break the job into more manageable chunks. Get the code working for one URL and then alter it for the more general case later on.

DarkEden Genesis

3:09 am on Jan 6, 2012 (gmt 0)

10+ Year Member



g1smd, I sent you a message here.

I also added you at Skype...please get in contact with me, check your inbox here.

Alejandro

3:17 am on Jan 6, 2012 (gmt 0)

10+ Year Member



Can you help me with the code ..? a code to include in general all

This is the code that I have put orita in htaccess:

RewriteRule ^view.php? Filename=$1(.*)$ViewImage/?[L,R=301]

lucy24

8:33 am on Jan 6, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You need to read up on Regular Expressions and query strings. I'm morally certain that you're not trying to find pages whose full URL, exclusive of query, is either

www.example.com/view.php

or

www.example.com/view.ph

and redirect them to

www.example.com/Filename= ....

uhmm. Not sure what you're aiming for here, unless you're trying to test your custom 500 page and make sure it displays as intended.

Alejandro

6:24 pm on Jan 6, 2012 (gmt 0)

10+ Year Member



It does not matter .. thanks