LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 02-02-2009, 11:49 PM   #1
addbbe
LQ Newbie
 
Registered: May 2006
Posts: 8

Rep: Reputation: 0
apache url rewrite about Canonical URLs get 404 error


In apache manual:
http://httpd.apache.org/docs/2.2/rew...ite_guide.html
Quote:
Canonical URLs

Description:

On some webservers there are more than one URL for a resource. Usually there are canonical URLs (which should be actually used and distributed) and those which are just shortcuts, internal ones, etc. Independent of which URL the user supplied with the request he should finally see the canonical one only.
Solution:

We do an external HTTP redirect for all non-canonical URLs to fix them in the location view of the Browser and for all subsequent requests. In the example ruleset below we replace /~user by the canonical /u/user and fix a missing trailing slash for /u/user.

RewriteRule ^/~([^/]+)/?(.*) /u/$1/$2 [R]
RewriteRule ^/u/([^/]+)$ /$1/$2/ [R]
so I setup apache 2.2.4 and use the default configuration file (httpd.conf) enable the user configuration file, delete the "#" befind "Include extra/httpd-user.conf"
I add something to httpd.conf file like this
Code:
RewriteEngine On
RewriteRule   ^/~([^/]+)/?(.*)    /u/$1/$2  [R]
RewriteRule   ^/u/([^/]+$  /$1/$2/   [R]
As same the apache manual, but it isn't work fine.
I got some error info in log file
Quote:
192.168.254.1 - - [02/Feb/2009:22:38:31 +0800] "GET /~testuser/ HTTP/1.1" 302 219
192.168.254.1 - - [02/Feb/2009:22:38:31 +0800] "GET /u/testuser/ HTTP/1.1" 404 207
192.168.254.1 - - [02/Feb/2009:22:38:37 +0800] "GET /u/testuser/ HTTP/1.1" 404 207
I am not sure something lost in my httpd.conf or other?
who can help me? give me an example configuration file is best
thank you very much

PS:
Code:
RewriteEngine On
RewriteRule ^/indx.html$  /index.htm
it work correct

Last edited by addbbe; 02-03-2009 at 01:43 AM.
 
Old 02-04-2009, 03:02 PM   #2
JimLebeau
LQ Newbie
 
Registered: Aug 2006
Posts: 8

Rep: Reputation: 0
Talking Rewrite Rules Rule!!

You have your rewrite rules messed. I think you took the from the wrong part of the manual.



RewriteEngine On
RewriteRule ^/~([^/]+)/?(.*) /u/$1/$2 [R]
RewriteRule ^/u/([^/]+$ /$1/$2/ [R]

The first rule will change /~joesmith/texts to /u/joesmith/texts/ but I am not sure what will happen with /~joesmith or with
/~joesmith/books/algebra


In your second rule you have not defined $1 or $2, you have unbalanced (

It should be RewriteRule ^/u/([^/]+)/(.*)$ /$1/$2/ [R]
and will change /u/lalalala/hahahaha to /u/lalalala/hahahaha/
 
Old 02-05-2009, 07:57 AM   #3
addbbe
LQ Newbie
 
Registered: May 2006
Posts: 8

Original Poster
Rep: Reputation: 0
Hi guy, thank you
it seem I lost the ) when I post it
I use the rule that you give me

RewriteEngine On
RewriteRule ^/~([^/]+)/?(.*) /u/$1/$2 [R]
RewriteRule ^/u/([^/]+)/(.*)$ /$1/$2/ [R]

it change /~user to /user//

I copy from the manual
RewriteRule ^/~([^/]+)/?(.*) /u/$1/$2 [R]
RewriteRule ^/u/([^/]+)$ /$1/$2/ [R]

it change /~user to /u/user/ OK
but I got 404 error as before.
 
Old 02-06-2009, 10:15 AM   #4
JimLebeau
LQ Newbie
 
Registered: Aug 2006
Posts: 8

Rep: Reputation: 0
I made a mistake in my second rule, it should be ^/u/([^/]+)/(.*)$ /u/$1/$2/ but even that may not be what you need.

what happes if you put http://<server>/u/user/ into the browser address window, so rewrite is not used? What can you see in the apache logs?
 
Old 02-08-2009, 02:39 AM   #5
addbbe
LQ Newbie
 
Registered: May 2006
Posts: 8

Original Poster
Rep: Reputation: 0
thank you for your notice
I will test use the rules that you give, then tell you the result.
 
Old 02-08-2009, 11:58 AM   #6
addbbe
LQ Newbie
 
Registered: May 2006
Posts: 8

Original Poster
Rep: Reputation: 0
when I put http://<server>/~user
the access.log display like these
192.168.254.1 - - [09/Feb/2009:01:52:55 +0800] "GET /~user HTTP/1.1" 302 219
192.168.254.1 - - [09/Feb/2009:01:52:55 +0800] "GET /u/user/ HTTP/1.1" 404 207

I put http://<server>/u/user
the access.log display
192.168.254.1 - - [09/Feb/2009:01:54:18 +0800] "GET /u/user/ HTTP/1.1" 404 207

the httpd.conf like below:
Code:
RewriteEngine On
RewriteRule   ^/~([^/]+)/?(.*)    /u/$1/$2  [R]
RewriteRule   ^/u/([^/]+)$  /u/$1/$2/   [R]
the url display is correct, but it seem not correct content.
I think maybe need alias, but if use alias why need rewrite?
thank you
 
Old 02-09-2009, 07:58 AM   #7
JimLebeau
LQ Newbie
 
Registered: Aug 2006
Posts: 8

Rep: Reputation: 0
Is there a url you can use to see the content for user on <server>?
 
Old 02-10-2009, 06:18 AM   #8
addbbe
LQ Newbie
 
Registered: May 2006
Posts: 8

Original Poster
Rep: Reputation: 0
without rewrite url HTTP://<server>/~user is okay
 
Old 02-11-2009, 08:43 AM   #9
JimLebeau
LQ Newbie
 
Registered: Aug 2006
Posts: 8

Rep: Reputation: 0
Is the apache server configured to accept HTTP://<server>/u/user/ ? If it fails, can you see the error in the error logs or in the access logs?
 
Old 02-12-2009, 05:21 AM   #10
addbbe
LQ Newbie
 
Registered: May 2006
Posts: 8

Original Poster
Rep: Reputation: 0
httpd.conf
Code:
erverAdmin you@example.com
DocumentRoot "/opt/httpd/htdocs"
<Directory />
    Options FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
</Directory>
<Directory "/opt/httpd/htdocs">
    Options Indexes FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>
<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>
<FilesMatch "^\.ht">
    Order allow,deny
    Deny from all
    Satisfy All
</FilesMatch>
ErrorLog logs/error_log
LogLevel warn
<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>
    CustomLog logs/access_log common
</IfModule>
<IfModule alias_module>
    Alias /u/*  /home/*/public_html
    ScriptAlias /cgi-bin/ "/opt/httpd/cgi-bin/"
</IfModule>
<IfModule cgid_module>
</IfModule>
<Directory "/opt/httpd/cgi-bin">
    AllowOverride None
    Options None
    Order allow,deny
    Allow from all
</Directory>
DefaultType text/plain
<IfModule mime_module>
    TypesConfig conf/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
</IfModule>
Include conf/extra/httpd-userdir.conf
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>
RewriteEngine On
RewriteRule   ^/~([^/]+)/?(.*)    /u/$1/$2  [R]
RewriteRule   ^/u/([^/]+)$  /u/$1/$2/   [R]
httpd-userdir.conf
Code:
UserDir public_html
<Directory /home/*/public_html>
    AllowOverride FileInfo AuthConfig Limit Indexes
    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
    <Limit GET POST OPTIONS>
        Order allow,deny
        Allow from all
    </Limit>
    <LimitExcept GET POST OPTIONS>
        Order deny,allow
        Deny from all
    </LimitExcept>
</Directory>
without rewrite when I put http://<server>/~user in firefox the access log
Quote:
192.168.254.1 - - [12/Feb/2009:19:16:04 +0800] "GET /~user/ HTTP/1.1" 200 30
192.168.254.1 - - [12/Feb/2009:19:16:04 +0800] "GET /favicon.ico HTTP/1.1" 404 209
192.168.254.1 - - [12/Feb/2009:19:16:07 +0800] "GET /favicon.ico HTTP/1.1" 404 209
with rewrite when I put http://<server>/~user in firefox it will change to http://<server>/u/user/. The access log display
Quote:
192.168.254.1 - - [12/Feb/2009:19:18:22 +0800] "GET /~user/ HTTP/1.1" 302 219
192.168.254.1 - - [12/Feb/2009:19:18:22 +0800] "GET /u/user/ HTTP/1.1" 404 207
the error log
Quote:
[Thu Feb 12 19:20:18 2009] [error] [client 192.168.254.1] File does not exist: /opt/httpd/htdocs/u
 
  


Reply

Tags
apache, canonical, rewrite, url


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache rewrite rule help - dynamic url to static page DukeLeto Linux - Software 3 11-12-2008 11:26 AM
apache url rewrite gallew Linux - Server 0 08-25-2006 03:54 AM
Why don't LQ rewrite urls to enhance search engine rankings? chr15t0 LQ Suggestions & Feedback 7 04-26-2006 05:49 AM
apache proxy server rewrite urls slackwarefan Linux - Software 0 10-19-2004 07:58 PM
Use apache to rewrite to external server and keep same browser URL ijsman77 Linux - Software 1 04-09-2004 07:32 AM


All times are GMT -5. The time now is 01:58 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration