?
Solved

difficult mod_rewrite question

Posted on 2005-03-15
7
Medium Priority
?
264 Views
Last Modified: 2010-03-04
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
Comment
Question by:hibbidiji
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 27

Expert Comment

by:caterham_www
ID: 13549609
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 13552779
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
 
LVL 1

Author Comment

by:hibbidiji
ID: 13562314
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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 51

Expert Comment

by:ahoffmann
ID: 13562693
>  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
 
LVL 1

Author Comment

by:hibbidiji
ID: 13562809
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
 
LVL 51

Accepted Solution

by:
ahoffmann earned 2000 total points
ID: 13562842
RewriteRule ^/archive/([\d]+)/([\d]+)/?$ /archive.php?y=$1&m=$2 [L]

# accordiong your example, I'don't see where username is comming from
0
 
LVL 1

Author Comment

by:hibbidiji
ID: 13562853
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question