htaccess removes the GET variables - php

I am editing a script and need to read GET variables to make my job done. But it seems the .htaccess file is manipulating it and removes everything at the end of custom URLs. I have no idea how to modify apache configurations to make it to works fine for the script yet me.
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ index.php?route=$1/$2 [L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?route=$1 [L]
</IfModule>
Here is a sample of URL I need to call:
http://domain.tld/controller/plugin/function/route?k1=v1&k2=v2
and the $_GET only contains one 'route' key with the value below:
controller/plugin/function/route
And other query strings are missed. What should I do for having them?

You probably want the QSA: Query String Append rewrite flag which does what it says on the tin.
For example:
RewriteRule ^(.*)/(.*)$ index.php?route=$1/$2 [L,QSA]

Related

URL Rewrite Module messing with $_GET variables

I've established a semi-working .htaccess file which alters this link:
localhost/profile.php?id=6
to this:
localhost/profile/6
However, this is adding .php to the end of my $_GET variables. I'm not very familiar with rewrite modules, .htaccess etc but this is very annoying. Couldn't find anything on the internet about it, if there is a different way to do it or fix it I would greatly appreciate any input.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profile/(.*) profile.php?id=$1
</IfModule>
Try restricting to only numbers:
RewriteRule ^profile/(\d*) profile.php?id=$1
Or exclude ., if the ID is not certainly numeric:
RewriteRule ^profile/([^.]*) profile.php?id=$1

Apache .htaccess rewrite for mutiple pages

I am working on a new script, I am using mod_rewrite to create permanent links. But I also want to have a login page that goes to a different main page.
ie.
http://www.example.com/dashboard
will load index.php
but
http://www.example.com/login will load login.php
currently my .htaccess looks like this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Not sure the best way to do what I would like. Tried to look around and couldn't find this example.
I think that all You need is just .php rewrite, so that You can use php pages with or without .php extension. The rest (redirecting) You should do from php scripts.
Here is how .htaccess should look like in this case.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [QSA,L]
</IfModule>

htaccess - Need help writing the correct rule

I'm using SimpleMVCFramework
All my routes are working fine, based on the default htaccess file:
Options -Indexes
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine On
RewriteBase /mpl/servicos/smvcf/
# Force to exclude the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ $1 [R=307,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>
I have a route that expects an id:
http://localhost/mpl/servicos/smvcf/detalhe/37343
But I need the URL the user sees to be friendly as:
http://localhost/mpl/servicos/smvcf/mercedes_benz-a-a_220_cdi_auto-37343.html
I thought something like this would work, but I get a 404:
RewriteRule ([^/]+)-([^/]+)-([^/]+)-([^/]+).html detalhe/$4
Please help.
The user sees the pretty url, but you are not interested in what the seo-title is. What you are interested in is the number that is hidden in it. As the number is in the very back of the pretty url, let's just match on that:
RewriteRule ^[^/]+-([0-9]+)\.html detalhe/$1 [L]
If you are trying to use another rewrite that will not be routed through the default MVC route index.php then you need to make sure you place your new rewrite before those rules. Also that folder you are redirecting to will need to have a index PHP file to do something as well.
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /mpl/servicos/smvcf/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)-([0-9]+)\.html detalhe/$2 [L]
# Force to exclude the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ $1 [R=307,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>

mod_rewrite issue with single file no query string

Trying to use mod-rewrite to alter the address bar versus the actual page that's being loaded. It works great when I have a query string to use, but I need to do it without a query string, just a straight up pretty url.
It seems to be that without a query string, straight mod_rewrites to files just shows the actual filename with extension.
Here's what my htaccess looks like:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
This works:
RewriteRule ^admin/account/([^/]*)/$ /adminconfig/account.php?id=$1 [L]
This doesn't:
RewriteRule ^admin/account/$ /adminconfig/account.php [L]
What's happening is that in the address bar I see:
example.com/adminconfig/account.php/
should be:
example.com/admin/account/
You need to do your rules like this. I also made sure multiviews was off
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin/([^/]+)/?$ /adminconfig/$1.php [L]
This rule should allow you to use a URL like this below in the address bar
example.com/admin/account/

htaccess rewrite rule to redirect to file or not

I have been trying to write a set of htaccess rules that will do the following:
With the url:
mydomain.com/blog/something
If a file called blog.php exists in
the web directory(the directory with
index.php in) then redirect to
blog.php?url=something
If blog.php does not exist, redirect
to index.php?url=blog/something
EDIT: mydomain.com/blog/something is an example, it could be any url string and the htaccess should search the web directory for a file corresponding to the first part of the url string, which in this case is "blog"
In both cases I can then use $_GET['url'] to get parameters from the url to intiate whatever actions in my php script.
I currently have:
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(.*)$ $1.php?url=$2 [L]
RewriteRule ^(.*)$ index.php?url=$1 [L]
</IfModule>
But unfortunately this is not working, I'm not sure why, I'm proficient enough with php but my htaccess skills are pretty limited. I don't know if the [L] option is the one I should be using or if I should be using more options. Currently whatever the url string is, $_GET['url'] returns "index.php".
If any more information is required just ask.
I hope someone can help
Regards
Luke
I hope that the following directives will work for you.
Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?.*$ - [E=FILE:%{DOCUMENT_ROOT}/$1.php]
RewriteCond %{ENV:FILE} !^$
RewriteCond %{ENV:FILE} -f
RewriteRule ^([^/]*)/?(.*)$ $1.php?url=$2 [QSA,L]
RewriteCond %{ENV:FILE} !^$
RewriteCond %{ENV:FILE} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Apache mod_rewrite Reference: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

Categories