Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 267
  • Last Modified:

difficult mod_rewrite question

I have a blogging script that takes GET input.   an example might be ?u in the first example.  

user.domain.com shoudl be rewritten to user.domain.com?u=user for instance.
I have been told that this code would function with my site but its not.   I have checked that mod_rewrite is working




RewriteEngine On

RewriteBase /

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI}^([^.]+)\.DOMAIN\.COM(:80)?<>/([^/]*) [NC]
RewriteCond %1<>%3 !^(.*)<>\1$ [NC]
RewriteRule ^page(\d+)/?$ /index.php?u=%1&page=$1 [L]

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI}^([^.]+)\.DOMAIN\.COM(:80)?<>/([^/]*) [NC]
RewriteCond %1<>%3 !^(.*)<>\1$ [NC]
RewriteRule ^profile/?$ /profile.php?u=%1 [L]

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI}^([^.]+)\.DOMAIN\.COM(:80)?<>/([^/]*) [NC]
RewriteCond %1<>%3 !^(.*)<>\1$ [NC]
RewriteRule ^archive/([^/]+)/([^/]+)/?$ /archive.php?u=%1&y=$1&m=$2 [L]

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI}^([^.]+)\.domain\.com(:80)?<>/([^/]*) [NC]
RewriteCond %1<>%3 !^(.*)<>\1$ [NC]
RewriteRule ^archive/([^/]+)?/?$ /archive.php?u=%1 [L]

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI}^([^.]+)\.domain\.com(:80)?<>/([^/]*) [NC]
RewriteCond %1<>%3 !^(.*)<>\1$ [NC]
RewriteRule ^archive/?$ /archive.php?u=%1 [L]

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI}^([^.]+)\.domain\.com(:80)?<>/([^/]*) [NC]
RewriteCond %1<>%3 !^(.*)<>\1$ [NC]
RewriteRule ^(\d+)/?$ /entry.php?u=%1&e_id=$1 [L]

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST}<>%{REQUEST_URI}^([^.]+)\.domain\.com(:80)?<>/([^/]*) [NC]
RewriteCond %1<>%3 !^(.*)<>\1$ [NC]
RewriteRule ^$ /index.php?u=%1 [L]
0
hibbidiji
Asked:
hibbidiji
  • 3
  • 3
1 Solution
 
caterham_wwwCommented:
the original intention of your snipped was the following:

a request of user.domain.de/123.php points via DNS-Entry to / (root)
/.htaccess should rewrite this request internally to /user/123.php

=> write subdomains in subdirs located at the main domain
domain.com => /
sub.domain.com => /sub/ this is equal to domain.com/sub/

is this the case for you, too?

If NOT (what seems to be the case: "user.domain.com shoudl be rewritten to user.domain.com?u=user") you can use
for .htaccess

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{QUERY_STRING} !^u=
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com
RewriteRule ^$ /?u=%2 [L]

for httpd.conf (outside <Directory>)

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.com
RewriteCond %{QUERY_STRING} !^u=
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.domain\.com
RewriteRule ^/$ /?u=%2 [L]
0
 
ahoffmannCommented:
you have syntax errors in most of your directives
RewriteCond cannot contain back-references like $1
RewriteRule uses $1, $2, etc. as back.reference and not %1, %2, etc.
0
 
hibbidijiAuthor Commented:
Ahoffman,
I'm going to post a WORKING htaccess instead and ask for help changing how it works:

my application USUALLY takes a url format like this:

domain.com/?u=username&p=25
(p is page)

I have written a php include that sets $_GET["u"] based on the subdomain:

http://username.domain.com
This will set ?u for me (thanks to my code)   In the htaccess below you will see that it has gone halfway, and is rewriting urls to this format:
http://www.domain.com/username/25
This would set $u and $p (as 25)
Further vars are set as well.   I can change how my script links to itself with no problem including the order of args that are passed.


RewriteEngine On
Options +Followsymlinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.* - [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/page(\d+)/?$ /index.php?u=$1&page=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/profile/?$ /profile.php?u=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/archive/([^/]+)/([^/]+)/?$ /archive.php?u=$1&y=$2&m=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/archive/([^/]+)?/?$ /archive.php?u=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/archive/?$ /archive.php?u=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ /entry.php?u=$1&e_id=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /index.php?u=$1 [L]

In some examples such as the profile request, I would like it to be http://username.domain.com/profile
In these instances, where there is only one argument, and its username, (get-u) I can fix this with my hack.  Other instances such as the archive, I would like it to be http://username.domain.com/archive/2004/12   (for december 2004)

suggestions?

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ahoffmannCommented:
>  such as the profile request,

RewriteRule ^([^/]+)/profile  http://$1.domain.com/profile.php?u=$1 [L]
# added   http://username.domain.com/  /not shure if this is what you want (assuming that username is $1)
# removed the trailing /?$ 'cause I assume that noone gives such an URL

RewriteRule ^([^/]+)/archive/([^/]+)?/?$ http://$1.domain.com/archive.php?u=$1 [L]
# not shure what you want here, missing an example
0
 
hibbidijiAuthor Commented:
allow me to elaborate.

I dont need the subdomain var to be assigned.   I am grabbing this with php.   what I am primarily concerned with is a framework to build on.

archive.php takes 3 arguments. $u (user)   $y (year) and $m (month)
I have already taken care of $u
I want a line that will look for all requests for  domain.com/archive/2004/12 and rewrite it into:  archive.php?y=2004&m=12

Using this information, I can make the rest of the lines I believe
0
 
ahoffmannCommented:
RewriteRule ^/archive/([\d]+)/([\d]+)/?$ /archive.php?y=$1&m=$2 [L]

# accordiong your example, I'don't see where username is comming from
0
 
hibbidijiAuthor Commented:
when archive.php is loaded, "subdomainhack.php" is loaded.   it contains code that parses the URL and assigns $_GET["u"] from the subdomain
I will try this now
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now