Forum Moderators: phranque

Message Too Old, No Replies

help with mod rewrite

         

Aibehn

8:16 am on Nov 20, 2008 (gmt 0)

10+ Year Member



Hi, i'm new in this forum, and also i'm a new user of the features of the mod_rewrite.

Here is my problem.

First, i have my domain: www.example.com, with two subdirectories ( /sub1 and /sub2 )

I have done a redirect from www.example.com to /sub2

Now, i want that the people who enter in the /sub1, be redirect to the www.example.com directory.
I have done this with this code:


RewriteEngine on
RewriteRule ^(.*)$ http://www.example.com/ [L,R=301]

This works, because, the .htaccess file is located in /sub1 directory. ( I asume that's because Base is /sub1 )

Now, i want that the people who enter into /sub2/modules.php?name=ForumsANDSOMEMORETEXTHERE, see the page with normality, and when a person want to enter on any page inside the /sub2 directory, i want to be redirected to www.example.com.

Note that, i want a redirection of all the files and querys inside /sub2 to www.example.com, however, when you the page:
/sub2/index.html nothing hapens and the same whit /sub2/modules.php?name=ForumsSOMETEXT pages.

I tried doing this:


RewriteEngine on

RewriteCond %{HTTP_HOST} !^modules\.php\?name=Forums [NC]
RewriteCond %{HTTP_HOST} !index\.html$ [NC]
RewriteRule (.*) http://www.example.com/ [L,R=301]

I tried doing as well with %{QUERY_STRING}, %{HTTP_HOST}, and %{REQUEST_URI}.

Any suggestions?

Thanks for reading.

PD. Sorry for my english, i'm spanish.

[edited by: coopster at 1:34 pm (utc) on Nov. 20, 2008]
[edit reason] one domain was missed, changed to example.com [/edit]

jdMorgan

2:26 pm on Nov 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure you really want a redirect. You may in fact want to do an internal rewrite for some or all of these.
The value of HTTP_HOST is only the hostname, such as "www.example.com"
The value of QUERY_STRING is only the query string appended to the requested URL, such as "name=Forums"
The value of REQUEST_URI is only the requested local URL-path, such as "/index.html"

So if you want to test or create back-references to various 'pieces' of the request sent to your server, you must use separate RewriteConds to do so.

You cannot use something like "RewriteCond %{HTTP_HOST} !^modules\.php\?name=Forums [NC]" because HTTP_HOST will never contain either "modules\.php" or "name=Forums". It will contain only the requested hostname, for example, "example.com".

If you have not already done so, find and read the mod_rewrite documentation, and the Apache URL Rewriting Guide. It is possible that you may be able to find a Spanish translation of these documents -- Search for this Apache documentation looking for search results from the Web sites of technical universities in Spain and other Spanish-speaking countries. Otherwise, all of the documentation is available in English from apache.org.

Also, find and read a Regular Expressions tutorial for help creating and using regular expressions for pattern-matching.

Links to the mod_rewrite documentation and to a regular-expressions tutorial (in English) are available in our Apache Forum Charter [webmasterworld.com].

You may also find the Apache section of our WebmasterWorld Library [webmasterworld.com] to be useful.

If you could describe you over-all goal here, that would simplify things. It is important to fully-define your requirements and your "plan" before moving into the coding phase.

It is best to look at the problem like this:

  • What is your current URL structure?
  • What is your current file structure?
  • How do you want to change your URL structure?
  • How do you want to change your file structure?

    Jim

  •