Abs Abs - 16 days ago 5
Apache Configuration Question

404 Error: Mod_Rewrite not working

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

Answer

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.

Comments