Reputation: 57916
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
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 "$"?!
Upvotes: 0
Views: 3474
Reputation: 137252
Use this to debug:
LogLevel trace8 rewrite:trace8
Obsolete options:
RewriteLog /tmp/mylog
RewriteLogLevel 9
Upvotes: 2
Reputation: 60378
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.
Upvotes: 0
Reputation: 655129
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.
Upvotes: 4