0

I managed to get some help from a previous question and I have the following in my .htaccess in my web root.

# REWRITE DEFAULTS
RewriteEngine On
RewriteBase /

# /view.php?t=h5k6 externally to /h5k6
RewriteCond %{THE_REQUEST} ^GET\ /view\.php
RewriteCond %{QUERY_STRING} ^([^&]*&)*t=([^&]+)&?.*$
RewriteRule ^/view\.php$ /%2 [L,R=301]

# /h5k6 internally to /view.php?t=h5k6
RewriteRule ^/(.*) /view.php?t=$1

However, if I type in this: http://www.example.com/7hde it will just give me a 404 error.

Am I missing something?

Thanks all

Update

This what I have now:

# /view.php?t=h5k6 externally to /h5k6
RewriteCond %{THE_REQUEST} ^GET\ /view\.php
RewriteCond %{QUERY_STRING} ^t=([0-9a-z]+)$
RewriteRule ^view\.php$ /%1 [L,R=301,QSA]

# /h5k6 internally to /view.php?t=h5k6
RewriteRule ^([0-9a-z]+)$ view.php?t=$ [L,QSA]

It seems to work but I can not make use of the Query string. Every time I try to get the value of t. I get this "$"?!

Abs
  • 56,052
  • 101
  • 275
  • 409
  • See also http://stackoverflow.com/questions/1079930/can-i-re-write-my-urls-like-this-and-is-it-a-good-idea – Gumbo Jul 07 '09 at 14:16

3 Answers3

4

When using mod_rewrite in .htaccess files (see Per-directory Rewrites), the contextual per-directory path prefix is stripped before testing a rule and appended after applying a rule.

In your case it is the leading / that needs to be removed from your patterns and substitutions:

# /view.php?t=h5k6 externally to /h5k6
RewriteCond %{THE_REQUEST} ^GET\ /view\.php
RewriteCond %{QUERY_STRING} ^([^&]*&)*t=([^&]+)&?.*$
RewriteRule ^view\.php$ /%2 [L,R=301]

# /h5k6 internally to /view.php?t=h5k6
RewriteRule ^(.*) view.php?t=$1

You forgot that information in your last question. Otherwise I would have told you that back then.

Community
  • 1
  • 1
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • Sorry Gumbo, I didn't know. I tried the new rules and I managed to get a 500 error, misconfiguration somewhere? – Abs Jul 07 '09 at 14:03
  • The last rule might cause an infinite loop as `view.php` is also matched by `.*`. Try to use a more specific pattern like I did (see http://stackoverflow.com/questions/1079930/can-i-re-write-my-urls-like-this-and-is-it-a-good-idea/1079947#1079947). – Gumbo Jul 07 '09 at 14:08
  • Ah ok - (for the last line) I am making use of this **RewriteRule ^([0-9a-z]+)$ view.php?t=$1 [L]** - it manages to rewrite to view.php. How can I add the query string. Also, I am trying get this rewrite to work for subfolders too I am not sure where to make the change for that?? – Abs Jul 07 '09 at 14:20
  • @Abs: Set the QSA flag to get the original query appended to the new one. And the second rule is already ready for use in a subfolder. – Gumbo Jul 07 '09 at 16:45
  • I have put the QSA flag: **RewriteRule ^view\.php$ /%2 [L,R=301,QSA]**. However, still can not use: **echo $_GET['t'];**. Sorry for the backwards and forward, but is there anything I am doing wrong? – Abs Jul 07 '09 at 17:29
  • I only get this "$" returned as the contents of the query string?? – Abs Jul 07 '09 at 17:32
  • Ok, I know the problem is the second rewrite rule as it is not getting the query string even when I give both the QSA flag. Any help? – Abs Jul 08 '09 at 19:33
  • Solved. I realised it needs to be $1 – Abs Jul 08 '09 at 19:46
2

Use this to debug:

LogLevel trace8 rewrite:trace8

Obsolete options:

RewriteLog /tmp/mylog
RewriteLogLevel 9
l0pan
  • 476
  • 7
  • 11
gahooa
  • 131,293
  • 12
  • 98
  • 101
0

It's rewriting http://www.example.com/7hde to http://www.example.com/view.php?t=7hde using the second rule. Then it's applying the first rule and changing the query string to http://www.example.com/7hde as the last rule, which is obviously invalid.

Get rid of the first rule.

Welbog
  • 59,154
  • 9
  • 110
  • 123