views:

348

answers:

3

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 "$"?!

A: 

Use this to debug:

RewriteLog /tmp/mylog
RewriteLogLevel 9
gahooa
+2  A: 

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.

Gumbo
Sorry Gumbo, I didn't know. I tried the new rules and I managed to get a 500 error, misconfiguration somewhere?
Abs
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
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
@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
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
I only get this "$" returned as the contents of the query string??
Abs
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
Solved. I realised it needs to be $1
Abs
A: 

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