HTACCESS friendly url and allow GET method - php

My code is
Options -Multiviews
RewriteEngine On
RewriteBase /
RewriteRule ^([a-z0-9-]+)\.html$ /index.php?cat=$1 [L]
If I access
mysite.com/name-of-category.html
it works, but if I access
mysite.com/name-of-category.html?anything=something
it shows the webpage but $_GET["anything"] shows nothing.

You must specify an option called QSA or 'Query String Append':
RewriteRule ^([a-z0-9-]+)\.html$ /index.php?cat=$1 [L,QSA]
It will ensure that the original query strings are also included as part of your new URL.

Related

Mod_rewrite handling extra get variables (QSA not working)

I have a mod rewrite rule for settings page: (localhost/settings/index.php)
RewriteRule settings/([a-zA-Z0-9_]+)/?$ settings/?path=$1 [QSA,L]
And I am handling menu references within the URL which will be similar to:
http://localhost/settings/xyz/?ref=menu1&abc=2&de=3..
However, with my current rewrite rule, the variable does not get passed along and I didn't get the values of ref,abc..
I read here about QSA flag but that doesn't seem to be working.
What am I doing wrong?
Try turning off MultiViews option:
Options -MultiViews
RewriteEngine On
RewriteRule ^settings/([\w-]+)/?$ settings/?path=$1 [QSA,L,NC]
In place of QSA, you can use this trick to captured query string:
RewriteRule ^settings/([\w-]+)/?$ settings/?path=$1&%{QUERY_STRING} [L,NC]

.HTACCESS - Prevention of 404 with no URL query

I'm using .htaccess to re-write some of my URLs for my web application. The following is used to make the user page a little prettier when user_urls (practically a URL friendly username) are passed to it:
RewriteRule ^user/([0-9a-zA-Z-]+) /user?url=$1 [NC,QSA]
When I input the URL http://example.com/user, the page redirects just fine, and the PHP handles the lack of the query string. However, when the server receives the request of http://example.com/user/, it returns a 404 error.
How can I prevent this from happening?
Thanks.
Redirect to the correct URL by prepending a rule
RewriteRule ^(.*)/$ /$1 [L,R=301]
or append /? to your regexp.
You can use these rules in your .htaccess:
Options -MultiViews
RewriteEngine On
RewriteRule ^user/?$ user.php [NC,L]
RewriteRule ^user/([0-9a-zA-Z-]+)/?$ user.php?url=$1 [NC,QSA,L]

.htaccess RewriteEngine with with various query strings

I have a site which can be open with multible get Resquests and without one.
So there is
?group=x , ?id=x, ?ha=x
All of them can be used at the same time, or only 1 or 2 of them.
I want to rewrite my http://www.example.com/test.php?....
to http://www.example.com/example
EXAMPLE:
So when i open www.example.com/test.php?group=x&id=y it should be displayed as www.example.com/example
And when i open www.example.com/test.php?ha=z
it should also display www.example.com/example
Is this possible with the mod_rewirte?
You could try putting a rule in your .htaccess file like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^test.php.*$ [NC]
RewriteRule ^test.php.*$ example [L]
</IfModule>
It should only apply to requests that start with /test.php and redirect all of them to http://wwww.yoursite.com/example .
You are going to lose your query string parameters with this method, unless you pass them using clean URLs, which you have to configure with htaccess as well.

.htaccess file not redirecting - guidance wanted

I have this url:
details
the url becomes in addressbar:
http://web228.sydney.webhoster.ag/soputnik/drive_to_london/?procid=12
but I want to process the logic in details.php.
How can I process the data in details.php in background and keep the first url in the address?
I tried this:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^drive_to_(.*)$ http://web228.sydney.webhoster.ag/soputnik/details.php [R=301,L]
but it is not working, error is: NOT FOUND
please help
Substitute your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^test/drive_to_(.*?)/?$ /test/details.php [L,NC]
If you don't want the URL to change, then it's not a redirect, just a rewrite.
Change:
RewriteRule ^/test/drive_to_(.*)$ /test/details.php [R=301,L]
To something like:
RewriteRule ^/test/drive_to_(.*)$ /test/details.php?city=$1 [L]
dont forget to pass any url params to details.php.
Rewriterule ^/drive_for_(.*)/\?(.*)$ http://domain.de/test/details.php/?$1 [R=301,L]
it seems relevant that you want to pass the city name as well, otherwise that context gets lost in the rewrite. If that is another url param you could pass it through to details.php as such
Rewriterule ^/drive_for_(.*)/\?(.*)$ http://domain.de/test/details.php/?$2&city=$1 [R=301,L]

.htaccess RewriteRule to preserve GET URL parameters

I'm having issues keeping the parameters of the URL working after an .htaccess URL rewrite.
My .htaccess rewrite is as follows:
RewriteEngine on
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?lang=$1&page=$2
Which means:
example.com/index.php?lang=en&page=product displays as example.com/en/product
For some reason, when I add a ?model=AB123&color=something at the end of my URLs I am not able to retrieve those parameters in PHP using $_GET['model'] and $_GET['color'] even though they are present in the displayed URL.
Why aren't the variables passed along?
You need to append with the [QSA] (query string append) tag. Try
RewriteEngine on
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?lang=$1&page=$2 [QSA]
See http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

Categories