0

I'm working on moving my .htaccess rules over to our vhost_ssl.conf file. I'm hitting some snags with rules that worked fine in .htaccess but aren't working in vhost. For example, in .htaccess this works fine:

RewriteRule ^example$ /example/ [L,R=301]
RewriteRule ^example/$ somewhere/something.php [QSA,L]

With the idea being that if someone visits example.com/example - they get redirected to example.com/example/ (just adding the trailing slash). Then the trailing slash version of the URL gets rewritten behind the scenes.

When I move this over to vhost:

RewriteRule ^/example/$ somewhere/something.php [QSA,L]

I hit a couple of snags:

  1. it turns into an infinite loop of redirects (my server stops it at 10), because the first rule is always triggered
  2. after I comment out the first rule, I get a "Bad Request" 400 error:

Bad Request Your browser sent a request that this server could not understand. Client sent malformed Host header

Any ideas why this might be happening? We're running apache/nginx if that means anything. I read another thread on SF for a similar issue, and the response was that it's a context issue and to add a leading slash to the final destination on the rewrite. But in my case when I do that:

RewriteRule ^/example/$ /somewhere/something.php [QSA,L]

The error changes to:

Unable to execute 'example/': No such file or directory

Also on the performance side of this in vhost, is there any difference between, for example:

RewriteRule ^/example/$ somewhere/something.php [QSA,L]

vs

RewriteCond %{REQUEST_URI} ^/example/$
RewriteRule .* somewhere/something.php [QSA,L]

Seems to me that the first version would be superior, it should accomplish the same goal but in less lines. But maybe having the RewriteCond separately makes it more efficient overall?

EDIT: Here's the vhost_ssl

RewriteEngine on
#canonocanconcical fix
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*) https://www.example.com$1 [R=301,L]
RewriteCond %{QUERY_STRING} !Per_Page=([0-9]+)
RewriteCond %{QUERY_STRING} show=([0-9]+)
RewriteRule c-([A-Za-z0-9-_]+)\.htm c-$1.htm?Per_Page=%1 [QSA,R=301]
RewriteCond %{QUERY_STRING} !CatListingOffset=([0-9]+)
RewriteCond %{QUERY_STRING} Offset=([0-9]+)
RewriteRule c-([A-Za-z0-9-_]+)\.htm /c-$1.htm?CatListingOffset=%1 [QSA]
  • Check your setting of RewriteBase. – Michael Hampton Apr 9 '18 at 19:04
  • there is no RewriteBase. Is one required? – Mike Willis Apr 9 '18 at 20:13
  • No, RewriteBase directive is available only in htaccess, not in the main config. – Luca Gibelli Apr 25 '18 at 19:43
0

You are overcomplicating, you can match the same url with and without a trailing slash using the ? quantifier and a single rewrite rule:

RewriteRule ^example/?$ http://example.com/somewhere/something.php [L,R=301]

To avoid loops, make sure that example != somewhere.

  • Thank you, unfortunately this isn't working. I don't know why, but nothing redirects/rewrites when I add that to my vhost, reconfigure, and refresh the page. Instead the browser stays at the entered URL (example.com/example) and shows the 404 page. Also, if this did work, wouldn't it do a 301 redirect to the destination page, instead of a rewrite? So the browser URL would change to /somewhere/something.php. I only want it to change to add the trailing slash. Once the trailing slash is there, I want it to be a behind the scenes rewrite. – Mike Willis Apr 25 '18 at 13:34
  • Can you post the whole vhost_ssl.conf file – Luca Gibelli Apr 25 '18 at 18:48
  • all set, it's part of the main question now – Mike Willis Apr 26 '18 at 15:15
  • I found the problem - it's not the rewrites at all, it's a problem with our ecommerce platform. The rewrites are working as intended, I just thought the errors I was getting were because the rewrites were wrong. They're not - the destination page (our ecommerce platform) doesn't understand them. If I send them to a normal .htm or .php page, they work fine. So unfortunately, there's no answer to this question :/ – Mike Willis Apr 26 '18 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.