Jump to content

Rewrite based on url var


Go to solution Solved by kicken,

Recommended Posts

I need to rewrite a url based on a certain url variable.  I am using the Slim3 framework also, just as reference to know what else the htaccess is doing.  I moved this site from a standard php version to a Slim3 mvc version, but I want the links that are on the internet right now to redirect accordingly.

 

Here is the url I need to rewrite: http://example.com/?make=Lexus&model=RX400h&year=3318&model_id=350

This is what I need it to be: http://example.com/vehicle?make=Lexus&model=RX400h&year=3318&model_id=350 (just with the word vehicle added)

 

I have tried several different rewrite rules and the closest I get is an infinite loop error.

RewriteCond %{QUERY_STRING} ^make=(.*)$ [NC]
RewriteRule ^(.*)$ /vehicle [NC,L,R=301]

#This is the Slim3 rewrite.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

I have also tried the below version

RewriteCond %{REQUEST_URI} !^/vehicle?make=
RewriteCond %{QUERY_STRING} ^make=(.*)$ [NC]
RewriteRule ^(.*)$ /vehicle [NC,L,R=301]

#This is the Slim3 rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
Link to comment
https://forums.phpfreaks.com/topic/304541-rewrite-based-on-url-var/
Share on other sites

  • Solution

Something like this probably:

 

RewriteCond %{QUERY_STRING} ^make=(.*)$ [NC]
RewriteRule ^$ /vehicle [NC,L,R=301]
The problem with using (.*) is that it will match /vehicle. You want the rule to only apply when the request is against the root (I'm assuming). If you need it against anything but /vehicle then try:

RewriteCond %{QUERY_STRING} ^make=(.*)$ [NC]
RewriteRule !^vehicle$ /vehicle [NC,L,R=301]

Well that was a simple fix.  BUUUUUT it is causing a major issue that I can't even comprehend.  So the rewrite works on my localhost but if I upload the change to my host it breaks the site.  Now here is the really bizarre thing, that little change is somehow changing what php version is being used for the site.  I have recently upgraded my WHM and cPanel with the ability to have a different php version for each domain.  I have this site I'm dealing with in this post set to php 7.0.21 and the rest of the domains set to 5.6.31 which is also the default setting for the server.
 

So I can run through the entire site multiple times with no issue, but as soon as I make the htaccess change and upload it it screws up the site and changes the php version.  The other bizarre thing is that the WHM stills shows the correct version being used at 7.0.21, but running a phpinfo file for the domain shows 5.6.31.  I can run through multiple pages and it won't work.  Then I revert the htaccess and it still won't work.  So then I simply go into the WHM and tell it to use 7.0.21 AGAIN (even though it already thinks it's using it) and the next page load works just fine.

I've never seen such a thing.  Here is the error that is being thrown by Twig.  From what I've gathered it's cause I'm using Twig 2.4 and any version of 2.x is not compatible with a php version less than 7. 

Type: Twig_Error_Runtime
Message: Failed to load Twig template "vehicle-selected.twig", index "": cache is corrupted in "vehicle-selected.twig".
File: /home/rstech/vendor/twig/twig/lib/Twig/Environment.php
Line: 377

There is more to the error that is shown on screen, but it's just stack trace.

When you configured the site for PHP 7, did it maybe add something to the .htaccess that got accidently removed when you uploaded the new one? That's the only thing I can think of that would cause that problem.

 

The PHP Version should really be set within the VirtualHost block and not via the .htaccess file, but I have no idea how WHM/cpanel might have configured things. I setup my configurations manually.

  • Like 1

I just checked and there are only 2 htaccess files on the entire folder structure on my host.  One is outside the doc root and only has a few lines in it that don't seem to point to anything with this issue.  The other file is mine and is unchanged from edits.

Actually Kicken you are right!  The files looked identical to me cause I hadn't checked what you said until after I changed the php version again and then re-uploaded my file, which override what WHM put in there.  I finally found what it was adding when I changed a couple other domains just now and then looked at the files right away.

 

This is what it adds at the bottom of the file. 

<IfModule mime_module>
  AddType application/x-httpd-ea-php70 .php .php7 .phtml
</IfModule>

I'm sure it only adds this as a reference to tell the domain what version to access.  Where if the version you're using is the default of the server, then the snippet isn't needed in there.

 

This is what it added to another domain when it did the main WHM update:

<Files 403.shtml>
order allow,deny
allow from all
</Files>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.