Forum Moderators: phranque

Message Too Old, No Replies

URL Redirect

url redirect how

         

major9

2:05 pm on May 7, 2009 (gmt 0)

10+ Year Member



Whenever a user put in the url like this -
http://example.com/abcdefghijklmnop
The above url will not have an extension.

I want it redirected to -
http://example.com/search.html?term=abcdefghijklmnop

I also have other urls in my htaccess, this shouldn't affect them.
I tried someways, but it ended in redirect loop & was stopped by the browser.

[edited by: jdMorgan at 3:26 pm (utc) on May 7, 2009]
[edit reason] example.com [/edit]

jdMorgan

3:26 pm on May 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please post the rule you tried to use as a basis for focused discussion.

Thanks,
Jim

g1smd

4:55 pm on May 7, 2009 (gmt 0)

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



*** I want it redirected to... ***

Do you want a redirect, or do you need a rewrite?

major9

8:12 am on May 8, 2009 (gmt 0)

10+ Year Member



I need a rewrite.

jdMorgan

1:56 pm on May 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It would be quite helpful if you would post your code as requested above, so that members here have some idea on where to start with corrections, explanations, and references to previous relevant threads...

Jim

major9

6:56 pm on May 8, 2009 (gmt 0)

10+ Year Member



Here is my code -

RewriteRule ^\(.*)$ [domain.com...]

When i tried accessing the site, it went into a loop & was stopped by the browser automatically.

g1smd

8:17 pm on May 8, 2009 (gmt 0)

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



That's because the "output" URL is matched again by the pattern and the output URL of that is again matched by the pattern... forever.

So, several things to consider.

What you have there is a 302 redirect. It's a redirect (even though you didn't include the [R] flag) because you have included a domain name in the target.

To change it to a rewrite, omit the domain name from the target and add the [L] flag to the end of the rule. A rewrite does not contain a domain name because we are taking a URL as input and targeting an internal filepath as a result.

With a redirect, you are telling the browser to go fetch a new URL. That is not what you want here.

The \ in the pattern is incorrect and unwanted.

You may also need to add a RewriteCond before your rule, one that looks at THE_REQUEST so that the rewrite only fires off for direct client requests.

Your pattern of (.*) is too wide. It matches every request. That is, a request for /robots.txt will be rewritten to /search.html?term=robots.txt - and it is a fair bet that your script will not return the proper robots.txt file that Google and other search engines are looking for. You need a more restrictive pattern here, not one that matches 'everything'.

jdMorgan

8:30 pm on May 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteRule ^([^./]+)$ /search.html?term=$1 [L]

means "Rewrite any requested URL-path that does not contain a period or a slash to the script file at /search.html with the requested URL-path as the query string."

Or perhaps you'd like:


RewriteRule ^(([^/]+/)*[^.]+)$ /search.html?term=$1 [L]

which means, "Rewrite any requested URL-path starting with zero or more (optional) directory levels, followed by a final part which does not contain a period, to the script file at /search.html with the requested URL-path as the query string."

In either case, the output cannot match the input and produce a loop, because neither of the rules' patterns will match if there is a period in the final path-part.

Jim