Forum Moderators: phranque

Message Too Old, No Replies

Mod Rewrite Problem

         

divine7

7:13 pm on Mar 30, 2012 (gmt 0)

10+ Year Member



I want to redirect (servers-side) following:

www.example.com/anydomainname.com

to

www.example.com/domain.php?q=anydomainname.com

The following code doesn't seem to work.

RewriteRule ^([a-zA-Z0-9-\.]+)$ domain.php?q=$1

Any suggestions?

[edited by: incrediBILL at 11:56 pm (utc) on Mar 30, 2012]
[edit reason] exemplified URLs [/edit]

lucy24

8:52 pm on Mar 30, 2012 (gmt 0)

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



I want to redirect

No, you don't. (I'm not reading your mind or giving you instructions. I'm fixing your vocabulary, because it's important.) You want to rewrite. User asks for a, you serve content from b.

What exactly do you mean by "doesn't seem to work"? It is obvious to you, but not to us. There are lots of ways* for a thing to not work.

If it did work, you would get an infinite redirect unless you have other Rules you haven't mentioned. Skim through the last few threads in this Forum and find the redirect-to-rewrite boilerplate. Is that what you want to do?

Then come back with your new improved Rules. (Dual. There will be two of them.)


* Up to 500, haha.

divine7

5:15 am on Mar 31, 2012 (gmt 0)

10+ Year Member



Yes, I want to rewrite.

--------------------------------------------------
I am able to rewrite the following

www.example.com/q=anydomainname.com

to

www.example.com/domain.php?q=anydomainname.com

by the following

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^q=([a-zA-Z0-9-\.]+)$ domain.php?q=$1
--------------------------------------------------

--------------------------------------------------
I am also able to rewrite the following

www.example.com/anyAlphanumericText

to

www.example.com/domain.php?q=anyAlphanumericText

by the following

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([a-zA-Z0-9-]+)$ domain.php?q=$1
--------------------------------------------------

--------------------------------------------------
But, I am not able to rewrite the following

www.example.com/anydomainname.com

to

www.example.com/domain.php?q=anydomainname.com

I am using following

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([a-zA-Z0-9-\.]+)$ domain.php?q=$1

The above rewrites it to

domain.php?q=domain.php

and it also rewrites my homepage to the same URL (which I don't want).
--------------------------------------------------

Really appreciate your help here.

g1smd

9:12 am on Mar 31, 2012 (gmt 0)

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



it also rewrites my homepage to the same URL

The terminology surrounding the use of mod_rewrite is necessarily exact and precise because by using the wrong terminology the reader may understand your question to be exactly backwards from what you actually want to do.

With a redirect, user asks for URL example.com/z.php?zzz and is redirected with a 30x status code to example.com/yyy and makes a new request for that new URL.

With a rewrite, user asks for example.com/yyy but the request is internally rewritten to fetch content from inside the server at location /z.php?zzz but without revealing that it has done so.

A redirect is therefore a URL to URL translation, and both items have an attached domain name.

A rewrite is a URL to server internal filepath translation and the latter item should be specified without mentioning a domain name.

When the question contains two URLs the reader is usually given to thinking a redirect is being asked for.

I don't understand what you mean about your home page. Please explain it again, mentioning what you request as a URL with domain name, what the browser address bar shows at the beginning and what it shows after the page is served, as well detailing the location inside the server (this one without domain name) of the file that contains the content that is served.

lucy24

10:10 am on Mar 31, 2012 (gmt 0)

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



Your home page shouldn't be affected at all, unless you are giving its URL explicitly as
www.example.com/index.html

instead of
www.example.com/

(Or unless mod_dir executes before mod_rewrite. That's out of your power if you're working on the htaccess level.)

Try all three of your rules again. Verify that the Regular Expression part of the rule is exactly the way you gave it here:

#1 ([a-zA-Z0-9-\.]+)
#2 ([a-zA-Z0-9-]+)
#3 ([a-zA-Z0-9-\.]+)

You don't need to escape periods inside grouping brackets. But what I'm more concerned about is the position of the literal hyphen in #1 and #3. And I'm going cross-eyed trying to find a difference between the two.

Matter of fact, if you're capturing the entire request (I assume the /q= variations were just for experimentation) all you need is

(.+)

By default, Regular Expressions start as soon as they can and go on as long as they can, so that's your whole request right there.

Except, of course, that you don't want to capture and rewrite everything.

You don't want to capture requests for robots.txt or sitemap.xml. You don't want to capture requests for javascript or stylesheets. You don't want to capture requests for images. All of those pass through the same mod_rewrite.

So figure out a rule that only captures requests for directories and files. Or for requests ending in dot com, it that's really the form they will have.

g1smd

10:55 am on Mar 31, 2012 (gmt 0)

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



This is where using extensionless URLs for pages really helps. If there's a period in the URL it will be served by a real file. If the URL ends with a slash, it's a real folder and the content will be silently served by the index file in that folder. If the URL has no period and doesn't end with a slash, then it is extensioness and the request will be rewritten and be served by a script inside the server.

lucy24

9:04 pm on Mar 31, 2012 (gmt 0)

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



To clarify the above for people reading along silently:

That is what will happen if you compose your RewriteRule appropriately. If you don't-- or if you have no Rewrites at all-- then a naked URL (neither slash nor extension) will serve a 404 :)