LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (http://www.linuxquestions.org/questions/linux-server-73/)
-   -   apache url rewrite about Canonical URLs (http://www.linuxquestions.org/questions/linux-server-73/apache-url-rewrite-about-canonical-urls-701811/)

addbbe 02-03-2009 12:49 AM

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

JimLebeau 02-04-2009 04:02 PM

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/

addbbe 02-05-2009 08:57 AM

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.

JimLebeau 02-06-2009 11:15 AM

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?

addbbe 02-08-2009 03:39 AM

thank you for your notice
I will test use the rules that you give, then tell you the result.

addbbe 02-08-2009 12:58 PM

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

JimLebeau 02-09-2009 08:58 AM

Is there a url you can use to see the content for user on <server>?

addbbe 02-10-2009 07:18 AM

without rewrite url HTTP://<server>/~user is okay

JimLebeau 02-11-2009 09:43 AM

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?

addbbe 02-12-2009 06:21 AM

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


All times are GMT -5. The time now is 08:24 PM.