views:

69

answers:

4

I am new to mod_rewrite and have been using a generator but it doesn't work. This is an example of what I am trying to achieve.

The original URL:
http://subdomain.domain.com/company.php?test=TES001

The rewritten URL:
http://subdomain.domain.com/company/AAA001

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule company/(.*)/(.*)/$ /company.php?$1=$2
ErrorDocument 404 /

Any help would be appreciated, thanks.

+1  A: 

I think your RewriteRule is written in the wrong way, kind of backwards, it should be

RewriteRule company.php?test=([A-Z]+)([0-9]+) /company/AAA$2

I.e. you first give the pattern to match and then what it is rewritten to... however I'm not sure if you can actually match the GET parameters like this...

antti.huima
Right. You can't match query string parameters like that. RewriteRule only look at the path (up to, but not including the question mark). To match the query string you can use `RewriteCond %{QUERY_STRING}`. See my answer.
Patrick McElhaney
A: 

I'm not a mod_rewrite expert, but:

RewriteRule company/(.*)/(.*)/$ /company.php?$1=$2

shouldn't match expressions like:

/company/foo/bar/

and map them into:

/company.php?foo=bar

You have, in your URL, just:

/company/foo

What are the Apache logs saying? Is your .htaccess actually read? Did you reload the Apache configuration? (can't remember if it is needed)

Roberto Aloi
+1  A: 

If you want /company.php?test=AAA001 to redirect to /company/AAA001, do this:

RewriteCond %{QUERY_STRING} test=([A-Z]+[0-9]+)  
RewriteRule ^company.php /company/%1? [R]    

If you want /company/AAA001 to be rewritten as /company.php?test=AAA001, do this:

RewriteRule company/([A-Z]+[0-9]+)$ /company.php?test=$1
Patrick McElhaney
Thanks, worked perfectly
account holder
A: 

You have the wrong order... immediately after "RewriteRule" is the form of the URL that you want (ie. your clean url), with the Regex for whatever may change in that url. After that, you have the path of the URL, plus the Regex tokens for the found values (ie. $1, $2, etc.)

RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

ErrorDocument 400 http://www.domain.com/400
ErrorDocument 403 http://www.domain.com/403
ErrorDocument 404 http://www.domain.com/404
ErrorDocument 500 http://www.domain.com/500

RewriteEngine on
RewriteBase /
#PAGES

RewriteRule ^/company/([A-Z]+)([0-9]+)/$ company.php?test=$1&%{QUERY_STRING} [NC,L]
AlishahNovin