Forum Moderators: phranque

Message Too Old, No Replies

How to remove "?" in redirect

         

jcmiras

9:15 am on Jul 6, 2005 (gmt 0)

10+ Year Member



When i redirect /page1.htm to page1 (no extension, no "/"), the url became /page1?. How do i remove the "?". my code is something like this in .htaccess;

redirect 301 /page1.htm [domain.com...]

jdMorgan

6:15 pm on Jul 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is the "?" in the requested URL? If not, then the first question to answer is, "Where is the code that is adding this question mark?" Rather that adding another layer of code as a band-aid fix, it would be better to find out where the question mark is being added, and fix the problem there.

As a final resort, you can use RedirectMatch in mod_alias or a mod_rewrite ruleset to remove the character.

Jim

jcmiras

7:10 pm on Jul 6, 2005 (gmt 0)

10+ Year Member



Actually, there`s no code that create that "?". when i redirect "page1.htm" to "newpage", "?" appears in the URL. For example, if i click a link "Click HERE" which has a URL of "www.mydomain.com/page1.htm", then i redirect it to "www.mydomain.com/newpage", the URL displayed becomes "www.mydomain.com/newpage?".

my URL redirect code is;
redirect 301 /page1.htm [mydomain.com...]

if i remove the "?" in
"redirect 301 /page1.htm [mydomain.com...]

the resulting URL becomes [mydomain.com...] which is my original URL that i have rewrited to [mydomain.com...]

Is there any possible solution to solve this problem?

Birdman

7:31 pm on Jul 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What may be happening is that the original querystring is being added to the new URL. Here is what the Apache docs say about Redirect

Redirect directive [httpd.apache.org]

Syntax: Redirect [status] URL-path URL
Context: server config, virtual host, directory, .htaccess
Override: FileInfo
Status: Base
Module: mod_alias
Compatibility: The directory and .htaccess context's are only available in versions 1.1 and later. The status argument is only available in Apache 1.2 or later.

The Redirect directive maps an old URL into a new one. The new URL is returned to the client which attempts to fetch it again with the new address. URL-path a (%-decoded) path; any requests for documents beginning with this path will be returned a redirect error to a new (%-encoded) URL beginning with URL.

Example:
Redirect /service [foo2.bar.com...]

If the client requests htp://myserver/service/foo.txt, it will be told to access htp://foo2.bar.com/service/foo.txt instead.

I would say the simple solution is to use mod_rewrite, rather than mod_alias.

RewriteEngine On
RewriteRule ^/page1\.htm$ /newpage [R=301,L]

Give that a try and see if it helps.

jcmiras

2:47 am on Jul 7, 2005 (gmt 0)

10+ Year Member



I've tried it. dont doesnt prompt a redirect.

jdMorgan

3:13 am on Jul 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> this in .htaccess

For use in .htaccess, delete the leading slash from the RewriteRule Pattern. You also may have to add the Options directive shown.


Options +FollowSymLinks
RewriteEngine On
RewriteRule [b]^p[/b]age1\.htm$ /newpage [R=301,L]

> Actually, there`s no code that create that "?"

Well, it cannot appear "by magic", so some code, somewhere is adding the "?". It is either in your .htaccess file, or the server is mis-configured; Ask your host.

Jim

jcmiras

3:43 am on Jul 7, 2005 (gmt 0)

10+ Year Member



do i have to put a "rewritecond"? then what should be the condition?

jdMorgan

3:52 am on Jul 7, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, as you describe your requirements, the only condition is that the URL matches the one specified in RewriteRule.

One other thought crossed my mind, though. It is possible that the "?" is being added due to content negotiation. If that is the the case, then the single line:


Options -MultiViews

might fix your original problem. Of course, you can only use that if your site does not use content negotiation.

Jim

jcmiras

4:31 am on Jul 7, 2005 (gmt 0)

10+ Year Member



I experimented and i solve the problem. Look what i`ve done.

1. if i redirect "page1.htm" to "newpage" using the following code,
redirect 301 /page1.htm [domain.com...]
the resulting URL becomes
[domain.com...] which is my problem earlier.

2. Now ro remove the "?cat=1", redirect it again using,

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /newpage\?cat=1\ HTTP/
RewriteRule ^dir/index\.php$ /newpage? [R=301,L]

I dont know what does "dir/index\.php" do, but it works.

3. My little problem here is that, i have so many redirects;
redirect 1: /dir/index.php?cat=1 -> /page1.htm (my old one)
redirect 2: /page1.htm -> /newpage?cat=1
redirect 3: /newpage?cat=1 -> /newpage

I dont think, Search engine bots will be happy for me. :(

Thnaks for the help guys.