Can't use "?" in URL because .htaccess - php

I have the following code into my .htaccess:
RewriteEngine On
Options -MultiViews
Options +FollowSymlinks
RewriteBase /testUrl/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L]
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
Then I catch the params using $_GET['page'] method.
This works:
localhost/testUrl/param1/param1
This DOES'NT works
http://localhost/testUrl/param1/param1?newparam=test&nextparam=test1
BUT THIS WORKS:
http://localhost/testUrl/param1/param1&newparam=test&nextparam=test1
I need to make working the 2nd example... with /param1/param2?newparam=etc
Any tips? Thank you so much guys

Thats because your rewrite rule doesnt include the QSA flag witch tells apache to append query string.
RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [QSA,R=301,L]
to read more about rewrite mod flags go to - http://httpd.apache.org/docs/2.4/rewrite/flags.html

Related

How to pass multiple parameter in .htaccess

I have url that I want to rewrite so it can pass the parameter like this
From www.example.com/questions.php/postid=101&title=htaccess_tuts
To www.example.com/questions.php/postid/1/title/htaccess_tuts
I have been trying to get this done using below .htaccess but i end up getting this
From www.example.com/questions.php/postid=1&title=htaccess_tuts
To www.example.com/questions.php/postid/101
The title part didn't show.
Can anyone help me pass it multiple parameter
RewriteBase /
#Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
#here is the problem i have don this but didn't work
#RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+questions\.php\?postid=([^\s&]+)&([^\s&]+) [NC]
#What can i do again?
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+questions\.php\?postid=([^\s&]+) [NC]
RewriteRule ^ questions/%1? [R=301,L]
RewriteRule ^questions/([^/]+)/?$ questions.php?postid=$1&title=$2 [L,QSA]
RewriteEngine On
RewriteRule ^questions\.php/postid/([^/]*)/title/([^/]*)$ /questions.php?postid=$1&title=$2 [L]
you can use this site for generate rewrite rules

Removing whitespaces or %20 in url using htaccess

hi guys can you help me here im having a little trouble here im tring to use the htaccess to remove all 20% in my url and replacing it with hyphen I manage to get rid the other 20% in between the words Acer,Liquid,S1,S510
here is my url /localhost/gadgets/product/Acer-Liquid-S1-S510%20Mobile
As you can see there is one %20 in last part, how can I remove it
And here is my htaccess
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /gadgets/
Options -Indexes
RewriteRule ^brand/([a-zA-Z]+)$ brand.php?id=$1
RewriteRule ^product/([A-Za-z0-9-]+)/?$ product.php?product_name=$1-$2 [NC,L]
RewriteRule ^(.*/|)[\s%20]+(.+)$ $1$2 [L]
RewriteRule ^(.+?)[\s%20]+(/.*|)$ $1$2 [L]
RewriteRule ^([^\s%20]*)(?:\s|%20)+(.*)$ $1-$2 [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Thanks in advance guys
Try this:
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /gadgets/
RewriteRule ^([^\s%20]*)(?:\s|%20)+(.*)$ $1-$2 [N,E=Redirect:1]
RewriteCond {ENV:Redirect} ^1$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R,L]
RewriteRule ^brand/([a-zA-Z]+)$ brand.php?id=$1 [NC,L]
RewriteRule ^product/([A-Za-z0-9-]+)/?$ product.php?product_name=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
The first rule removes all the whitespaces in a loop (using [N]) so we don't have to specify multiple RewriteRules doing it one by one now.
The next rule (with {ENV:Redirect} condition) is optional and is used to reflect the use of hyphens on the client's browser as well so that any bookmarks created link to the correct non-whitespaced version of the URL.

Unable to write a rewrite rule in .htaccess for my php website

My website is v2.example.com and I am trying to write a .htaccess rule but unable.
I want this : v2.example.com/ABCDEFGH
And I want to get the value ABCDEFGH as a parameter like it is v2.example.com/index.php?id=ABCDEFGH
can anyone please help me sorting out this problem.
I tried this :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.*) index.php?id=$1 [L]
</IfModule>
Please help me.
Have your rule like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+index\.php\?id=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?id=$1 [L,QSA]
</IfModule>
RewriteEngine on
RewriteRule ^(.*)$ index.php?id=$1 [L,QSA]
this will crash as it goes into infinite loop, as index.php is further rewritten to index.php?id=index.php and so on
solution
RewriteEngine on
RewriteRule ^([^_]*)$ _index.php?id=$1 [L,QSA]
that is create page _index.php, and rewrite all pages not having _ to this page, this way it will not go into infinite loop, you can choose ~ instead of _ if you think you will need _ in ur parameters

Turn mod_rewrite rules into PHP rules

I want to be able to handle mod rewrites from within PHP instead of my .htaccess file, this way when I have custom modules that need a new rewrite I don't have to redo the .htaccess file.
I want to mimic the way that WordPress does their .htaccess which is:
RewriteEngine On RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php/$1 [L,QSA]
My current .htaccess is this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# MBP Rules
RewriteRule ^module/([A-Za-z-_]+)/([A-Za-z-_]+)/?$ /index.php?p=module&prefix=$1&module_page=$2 [NC,QSA,L]
RewriteRule ^module/([A-Za-z-_]+)/([A-Za-z-_]+)/([A-Za-z-_]+)/?$ /index.php?p=module&prefix=$1&module_page=$2&page=$3 [NC,QSA,L]
RewriteRule ^module/([A-Za-z-_]+)/([A-Za-z-_]+)/([0-9]+)/?$ /index.php?p=module&prefix=$1&module_page=$2&id=$3 [NC,QSA,L]
RewriteRule ^([A-Za-z-_]+)/?$ /index.php?p=$1 [NC,QSA,L]
RewriteRule ^([A-Za-z-_]+)/([A-Za-z-_]+)/?$ /index.php?p=$1&s=$2 [NC,QSA,L]
RewriteRule ^([A-Za-z-_]+)/([A-Za-z-_]+)/([0-9]+)/?$ /index.php?p=$1&s=$2&id=$3 [NC,QSA,L]
Does anyone know how to make this happen?
My way is to replace
RewriteRule ^(.+)$ /index.php/$1 [L,QSA]
With
RewriteRule ^(.+)$ /index.php?path=$1 [L,QSA]
Then you have to do similar parsing and everything as modrewrite does, but you can do it yourself using preg_match on $_GET['path']. I.e.
if (preg_match('/id/([0-9]*)', $_GET['path'], $matches)) {
Do code;
}
Sure, have the .htaccess file look like the first example you have, and build your script from there.
Probably write some sort of Router class, to route the requests to their places, the fundamentals being splitting the received query by /, which gives you an array of URL parts, and start conditioning.

htaccess RewriteEngine

I have an url which is http://www.urlbookmarking.com/bookmarks-details.php?bid=55
and I want it to be like
http://www.urlbookmarking.com/bookmark/55
I wrote in my htaccess:
RewriteEngine on
RewriteRule /bid/(.*) bookmarks-details.php?bid=$1
But when I go to the first URL the rewrite engine does not apply my rule. Is there any mistake, or conflict somewhere?
My full htaccess file written as follows
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^urlbookmarking.com [NC]
RewriteRule ^(.*)$ http://www.urlbookmarking.com/$1 [L,R=301]
RewriteEngine on
RewriteRule /bid/(.*) bookmarks-details.php?bid=$1
Please help me.
The line Options +FollowSymLinks is optional if already configured in httpd.conf
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^urlbookmarking\.com [NC]
RewriteRule ^(.*)$ http://www.urlbookmarking.com/$1 [R=301, L]
RewriteRule ^bookmark/([0-9]+)$ bookmarks-details.php?bid=$1 [NC, L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
A few things:
RewriteEngine On only needs to called once, though this may not be causing any problems
I also have RewriteBase / after my RewriteEngine On line
My rewrite rule looks like this: RewriteRule ^common/(.*)$ common.php?file=$1 [QSA,L], which tells me that your rule should looke like this RewriteRule ^bookmark/(.*) bookmarks-details.php?bid=$1 [QSA,L]
you should use only one RewriteEngine on
RewriteRule /bid/(.*) bookmarks-details.php?bid=$1 - put this line after Options +FollowSymLinks
Try again
Do you want you url to be /bid/55 or /bookmark/55? because you have written it as if it is going to be /bid/55...
Anyway, your .htaccess should look more like this:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^urlbookmarking.com [NC]
RewriteRule ^(.*)$ http://www.urlbookmarking.com/$1 [L,R=301]
RewriteRule ^bid/(.*)$ bookmarks-details.php?bid=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
...without the multiple RewriteEngine on directives (these don't break anything but are unnecessary), and without the leading forward slash on the bid rewrite rule. Also, put your new rule before the rules that rewrites for non-existent file so it doesn't rewrite your URL before you get a chance to use it, and add a [L] flag to the rule so it doesn't get further modified by the other rules. Also, add the line start/end markers (^/$) to the rule.
You would only use the leading forward slash if you were putting the rules in httpd.conf, you don't use them in .htaccess files.
If you want your urls to be /bookmark/, just replace bid with bookmark.
This should redirect all '/bookmarks-details.php\?bid=(id)' urls with bookmarks ids (that have only numbers) to /bookmark/(id).
RewriteRule ^/bookmarks-details\.php\?bid=([0-9]+) /bookmark/$1 [R, NC, L]
Once you successfully rewritten the URL, you then need to write a companion rule to process it, like so:
RewriteRule ^/bookmark/([0-9]+) /bookmarks-details\.php\?bid=$1 [NC, L]
If should go between the rule that always adds 'www' to the beginning and the catch all rule, which I placed at the end. All together, it may look like so:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^urlbookmarking.com [NC]
RewriteRule ^(.*)$ http://www.urlbookmarking.com/$1 [L,R=301]
RewriteRule ^/bookmarks-details\.php\?bid=([0-9]+) /bookmark/$1 [R, NC, L]
RewriteRule ^/bookmark/([0-9]+) /bookmarks-details\.php\?bid=$1 [NC, L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This link may make things clearer: http://corz.org/serv/tricks/htaccess2.php

Categories