3

I have the following problem. We used many different URLs to the same page. Now we want to use only one URL for those pages. Here an example:

RewriteRule ^(subfolder1/folder1/|(subfolder2|subfolder3)/folder2/|folder3/)?(name1|name2|name3|name4)$ scriptname.php [QSA,NC]

As you can see it is pretty messy. What I now want to do is the following: Rewrite all URLs to only one of those URLs (e.g. subfolder1/folder1/name1) using a 301 and than using a rewrite on that URL to address the actual script. It might look like this:

RewriteRule ^((subfolder2|subfolder3)/folder2/|folder3/)?(name2|name3|name4)$ /subfolder1/folder1/name1 [R=301,QSA,NC]
RewriteRule ^subfolder1/folder1/name1$ script.php [QSA.NC]

Until here I have no problem. But now comes the tricky part. We use several development machines on UNIX and Windows machines. They all have different host names and folder. Here are some examples:

http://www.example.com (production)
http://test.example.com (testing)
http://localhost/development_folder/ (development WIN)
http://localhost:8888/development_folder/ (development MAC)

The issue is, that as we have subfolders on the development machines, I can't use an absolute URL as /subfolder1/folder1/name1/ as it would e.g. point to http://localhost/subfolder1/folder1/name1/ and not to http://localhost/development_folder/subfolder1/folder1/name1/ so all the rewrites would be broken on the development machines.

Is there any chance to get this issue working? As the folder development_folderis the same on all development machines, would it help to exclude/include that folder to the rewrites afterwards like this:

RewriteRule ^((subfolder2|subfolder3)/folder2/|folder3/)?(name2|name3|name4)$ /development_folder/subfolder1/folder1/name1 [R=301,QSA,NC]
RewriteRule ^development_folder/(.*)$ $1 [QSA.NC]
RewriteRule ^subfolder1/folder1/name1$ script.php [QSA.NC]

Or is there a better way of doing it? Any hint would help a lot.

2ndkauboy
  • 9,302
  • 3
  • 31
  • 65

3 Answers3

1

I would use Apache Include directives to modify the rule set. That way I can test the rules that will be used in production.

I don't remember if there is an issue with resolving the include wildcard to include no files, but if there is you could use an empty file in production and the actual dev rewrite rules one in dev.

RewriteRule ^((subfolder2|subfolder3)/folder2/|folder3/)?(name2|name3|name4)$ /subfolder1/folder1/name1 [R=301,QSA,NC]
Include /etc/apache/conf.d/*_dev.rewrites
RewriteRule ^subfolder1/folder1/name1$ script.php [QSA.NC]

On you dev machines and ONLY on your dev machine have the /etc/apache/conf.d/my_dev.rewrites:

RewriteRule ^development_folder/(.*)$ $1 [QSA.NC]
dietbuddha
  • 8,556
  • 1
  • 30
  • 34
  • OK, and if I have diffrent DEV machines (MAC and Windows) can I use a statement before the "Include" to handle that? I am not very familiar with Apache files. – 2ndkauboy Nov 22 '10 at 13:47
  • BTW: Sorry for being to late with my upvote so you haven't recieved the bounty. I therefor upvoted 10 of your answers to give you the well owned reward :) – 2ndkauboy Nov 22 '10 at 13:52
  • You can have mac_dev.rewrites and windows_dev.rewrites on the same machine or on different machines. They will both get pulled in. – dietbuddha Nov 22 '10 at 19:11
0

Wouldn't per-directory RewriteRules (inside an .htaccess) work? Then you can specify either a relative substitution (without a / in front) or, in case you need it, an absolute one (with /).

With the relative one, the base path will be stripped before rewriting and then prefixed again. Perhaps, you'll need to tune RewriteBase for this to work correctly (hmm, differently for your different servers...).

imz -- Ivan Zakharyaschev
  • 4,921
  • 6
  • 53
  • 104
0

I think there is a more deep problem with that dev/production servers layout but I suppose you have your reasons. This can help you:

# dev
RewriteCond %{HTTP_HOST} ^localhost$ [NC]
RewriteRule ^((subfolder2|subfolder3)/folder2/|folder3/)?(name2|name3|name4)$ /subfolder1/folder1/name1 [R=301,QSA,NC]
RewriteRule ^subfolder1/folder1/name1$ script.php [QSA.NC]

# production
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^((subfolder2|subfolder3)/folder2/|folder3/)?(name2|name3|name4)$ /dev_folder/subfolder1/folder1/name1 [R=301,QSA,NC]
RewriteRule ^dev_folder/subfolder1/folder1/name1$ script.php?dev=1 [QSA.NC]

The idea is to use the hostname to have a different set of rules. You can use dev1.example.com, dev2.example.com, etc as needed adding that 'fake' domains to /etc/hosts as alias of 127.0.0.1

djromero
  • 19,551
  • 4
  • 71
  • 68