Forum Moderators: phranque

Message Too Old, No Replies

How do I rewrite search domain to www.domain.com/domain.com

rewrite url and capture search value

         

tomsbn

3:44 am on May 20, 2010 (gmt 0)

10+ Year Member



hi I had a tough time finding a solution for this

I have a search page which user enter some domain name called somedomain.com then it should rewrite in address bar called www.domain.com/somedomain.com

with below code I am able to rewrite in address bar but not able to capture the search value...can anyone tell me where I am missing.

RewriteCond %{THE_REQUEST} /search\.php\?name=(www\.)?([^/\ ]+)[^\ ]*\ HTTP/
RewriteRule ^search\.php$ http://www.example.com/%2? [R=301,L]

RewriteCond %{HTTP_REFERER} ^((www\.)?example\.com.*)?$
RewriteRule ^(.*)$ search.php?name=$1 [L]

Thanks

Tom

[edited by: jdMorgan at 4:02 am (utc) on May 20, 2010]
[edit reason] example.com [/edit]

jdMorgan

4:35 am on May 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have explicitly cleared the query string by ending your substitution URL with a question mark...

RewriteCond %{QUERY_STRING} ^name=(www\.)?([^.:/]+(\.[^.:/]+)+)\.?(:[0-9]{1,5})?(/.*)?$
RewriteRule ^search\.php$ http://www.example.com/[b]%2?[/b] [R=301,L]

would be more efficient and might work better (without the question mark on the substitution path).

This pattern accepts (but discards) optional final periods on the hostname (FQDN format), port numbers, and trailing slashes.

Your second rule appears to have the potential to cause an 'infinite loop', in that all requests --including those for search.php as a result this rule being previously-executed-- will be rewritten to your search script. In other words, it rewrites search.php to itself, and the "name" variable will end up as "name=search.php" after the second iteration. Your server will likely also be slow...

If nothing else, you might want to exclude requests for search.php itself, robots.txt, custom error documents, sitemap.xml, Google/Yahoo/Bing Webmaster tools validation keys, images, CSS stylesheets, external JavaScripts, media files (e.g. .swf, .flv), and document files (e.g. PDF) from being rewritten by this rule. Example:

RewriteCond %{HTTP_REFERER} ^((www\.)?example\.com.*)?$
RewriteCond $1 !^(search\.php|robots\.txt|(403|404|410)error\.html)$
RewriteCond $1 !\.(gif|jpe?g|png|ico|css|js|xml|swf|flv|avi|pdf)$
RewriteRule ^(.*)$ search.php?name=$1 [L]

Jim

[edit] Correction as noted below. [/edit]

[edited by: jdMorgan at 1:23 pm (utc) on May 20, 2010]

tomsbn

5:13 am on May 20, 2010 (gmt 0)

10+ Year Member



Hi JIm

Thanks for instant reply....but none of these working correctly

according to your second rule

RewriteCond %{HTTP_REFERER} ^((www\.)?example\.com.*)?$
RewriteCond $1 !^(search\.php|robots\.txt|(403|404|410)error\.html)$
RewriteCond $1 !\.(gif|jpe?g|png|ico|css|js|xml|swf|flv|avi|pdf)$
RewriteRule ^(.*)$ search.php?name=$1 [L]

this consider everything as value and captured in GET. if I exclude some file say search.html then it
wont capture the value search.html fires.

If try your query string mode then it repeats the same search query..example:

RewriteCond %{QUERY_STRING} ^name=(www\.)?([^.:/]+(\.[^.:/]+)+)\.?(:[0-9]{1,5})?(/.*)?$
RewriteRule ^search\.php$ http://www.example.com/%2 [R=301,L]

output is:

http://www.example.com/google.com?name=google.com

jdMorgan

1:28 pm on May 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For the second rule, I forgot to clear the query string. See corrections to code posted above (Question mark added to substitution)

I don't understand what your're saying here:
this consider everything as value and captured in GET. if I exclude some file say search.html then it
wont capture the value search.html fires.


If you exclude "search.html", then requests for the URL example.com/search.html will not be rewritten and will not be passed to your script. Instead, the file /search.html will be served if it exists, or a 404-Not Found response will be served if /search.html does not exist.

Without exclusions, your script must generate correct responses for all of the example files and filetypes I listed. So it must generate all images and css files used on the site, and produce a correct robots.txt file, sitemap.xml file, etc.

Jim

jdMorgan

1:40 pm on May 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Never mind, I see what you're trying to do here. Lack of comments led to some confusion...

Let's leave the image and other exclusions for now, and start with a simple second rule. Try this:

# Externally redirect all direct client requests
# for script URL to search-engine-friendly URL
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /search\.php\?name=(https?://)?(www\.)?([^.:/\ ]+(\.[^.:/\ ]+)+)\.?(:[0-9]{0,5})?(/[^\ ]*)?\ HTTP/
RewriteRule ^search\.php$ http://www.example.com/%3? [R=301,L]
#
# Internally rewrite search-engine-friendly URL requests
# to script filepath unless previously rewritten
RewriteCond %{HTTP_REFERER} ^((www\.)?example\.com.*)?$
RewriteCond $1 !^search\.php$
RewriteRule ^(https?:)?(www\.)?([^.:/]+(\.[^.:/]+)+)\.?(:[0-9]{0,5})?(/.*)?$ search.php?name=$3 [L]

Example for the first rule:
Client (user) requests
http://example.com/search.php?https://www.google.com.:80/junk -or-
http://example.com search.php?google.com
First rule redirects client to http://example.com/google.com

For the second ruie:
Client requests
http://example.com/https://www.google.com.:80/junk -or-
http://example.com/google.com
Rule rewrites to /search.php?name=google.com

Jim

tomsbn

8:46 pm on May 20, 2010 (gmt 0)

10+ Year Member



JIm I did not tested the new one... will let you know by tomorrow..thanks for the help

Tom