0

I have the following RewriteMap function:

RewriteMap map_company txt:/var/www/vhost/domain.com/httpdocs/map_company.txt

I am trying to rewrite my index.php?shop_id=1 to /company-name/

so my map_company.txt file contains: company-name 1

I cannot seem to get it working. Here is my htaccess file:

# tried this
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?shop_id=${map_company:$1} [NC,L,QSA]

#and this
RewriteRule ^(\d+)/$ index.php?shop_id=${map_company:$1} [NC,L,QSA]

If I do that then I get the error: File does not exist: /var/www/vhosts/domain.com/httpdocs/company-name

Does anyone have any ideas? I also need to make sure it doesn't affect my standard folders like "css, js, images".

Dagg Nabbit
  • 75,346
  • 19
  • 113
  • 141
Drew
  • 6,736
  • 17
  • 64
  • 96
  • While not the cause of the `file does not exist` error, the arguments in your `map_company.txt` rewrite map file are reversed. The first argument on each line is the value to match while the second is the replacement value. http://httpd.apache.org/docs/2.4/rewrite/rewritemap.html#txt – curiouser Apr 23 '18 at 14:51

2 Answers2

1

Looks like you can't declare a RewriteMap in an .htaccess file:

The RewriteMap directive may not be used in sections or .htaccess files. You must declare the map in server or virtualhost context. You may use the map, once created, in your RewriteRule and RewriteCond directives in those scopes. You just can't declare it in those scopes.

https://httpd.apache.org/docs/2.4/rewrite/rewritemap.html

Tim Beadle
  • 307
  • 3
  • 5
1

Have you turned on the rewrite engine? RewriteEngine on needs to be defined. Also, check you have the necessary AllowOverride value for this folder to let you do this.

As an aside, I'd consider making the trailing slash optional:

RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?shop_id=${map_company:$1} [NC,L,QSA]
daiscog
  • 11,441
  • 6
  • 50
  • 62
  • Thanks for your help. Yes RewriteEngine is on. I get the following error: No such file or directory: mod_rewrite: can't access text RewriteMap file /var/www/vhost/domain.com/httpdocs/map_company.txt - the file is there though and I aksed my host and they said the permissions are set correctly so they don't know what is happening either. What could that be? – Drew Oct 14 '11 at 15:09
  • What about the permissions of the directory? It'll need to be executable (as will all parent directories). Have you tried moving the file elsewhere? – daiscog Oct 14 '11 at 15:16