URL Rewrite Module Questions - php

I want to make a rewrite module in htaccess file something like below
http://example.com/index.php?tel=604-567-0909&cat=1&sort=2
to
http://example.com/604-567-0909/?cat=1&sort=2
so $_GET will be
$_GET[tel] = 604-567-0909
$_GET[cat] = 1
$_GET[sort] = 2
How would this rule look like?

It would look like this if you only wanted to accept numbers and dashes in the tel field:
RewriteRule ^/?([0-9\-]+)/$ index.php?tel=$1 [L]
The other values in the query string would be carried over automatically, and this rule would work without them just fine.

Related

I want to remove query string variable and replace it query value in php

$url = example.com/your-new-key-is.php?key=5&submit=success
The above url consists query string and submit value, also it's not user friendly
Once I click the submit the new url want to be like that
$url = example.com/your-new-key-is-5/
I want to replace the key url with value(5) and truncate the part followed & symbol.
Anybody knows the answer ?
I think you can:
$url1 = example.com/your-new-key-is.php?key=5&submit=success
$url2 = example.com/your-new-key-is-5/
<?php
$url= str_replace($url1 , $url2);
?>
You can see more examples here: function.str-replace.php
You need to use mod rewrite. There are ways to write them. You can also use a rewrite rule generator to understand how its done.
Below are the rules that needs to go into the .htaccess file
# Rewrite --- example.com/your-new-key-is.php?key=5&submit=success => example.com/your-new-key-is-5/
RewriteCond %{QUERY_STRING} (^|&)key=5($|&)
RewriteCond %{QUERY_STRING} (^|&)submit=success($|&)
RewriteRule ^example\.com/your-new-key-is\.php$ /example.com/your-new-key-is-5/?&%{QUERY_STRING}
Here is a link to the Generator.
http://www.visiospark.com/mod-rewrite-rule-generator/
After you've written the rule to htaccess, you will enter in example.com/your-new-key-is-5/ which is equivalent to example.com/your-new-key-is.php?key=5&submit=success

Change Of Complex To Simple URL & Remove Index.php

I have been trying & searching alot but couldn't find any solution for i have tried many things in .htaccess
My website's Url
mysite.org/torrents.php
To this
mysite.org/Browse
This
mysite.org/torrents.php?parent_cat=Movie
to this
mysite.org/Movie
This
mysite.org/account-details.php?id=737
to this
mysite.org/user/username
Your links won't all work properly, i.e. the /user/username one won't work to an ID number, you could pass the username through then do a lookup based on the username, but htaccess won't know that username "tim" is id number "123"
This should work:
RewriteEngine On
RewriteRule "user/([0-9]*)" account-details.php?id=$1
RewriteRule "category/([a-zA-Z0-9_-]*)" torrents.php?parent_cat=$1
RewriteRule "browse" torrents.php
RewriteRule "" torrents.php
With that I've separated out the categories into a subfolder called 'category' this makes it far easier to allow any category name you want.
For the regular expression matches in the RewriteRules theres an excellent free program called 'Expresso' that will help you build up regular expressions and test them against a list of values to see if they match, I'd recommend searching for it and getting a copy as it's very handy.
Also for the username part if you wanted to do username instead of user ID numbers, then swap:
RewriteRule "user/([0-9]*)" account-details.php?id=$1
to this:
RewriteRule "user/([a-zA-Z0-9_-]*)" account-details.php?username=$1
Try using this. it is a htaccess generator. If this doesn't work you will need to check you php.ini settings to see if it allows this type of link.

CodeIgniter Route rsegment() Duplicates End Value

Using CodeIgniter route I have this rule
$route['^(?!login|profile|signup|notification|member).*/(:num)/(:any)/(:any)'] = "home/$2/$0/$1/$3";
My URL would be something like this http://example.com/memberfolder/7/view_document/WjbZpAPHM6
So basically the code should route to view_document() method in the home class with rsegment(5) being value WjbZpAPHM6
However, when the code goes to the home class, rsegment(5) value is view_document.
As you can see for my routing rule I should only have 5 rsegments, so when I printed all rsegments I got this:
home view_document memberfolder 7 view_document WjbZpAPHM6
As you can see my rsegment(5) is a duplicate or rsegment(2). Even though I should have only 5 rsegments, my document file value WjbZpAPHM6 is stored in rsegment(6).
Any advice on how to correct rsegment(5) to be the value of the document name rather then view_document
Also, my .htaccess is as such:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /example/index.php/$1 [L]
(:any) will match any character EXCEPT / in CI 3.0 or WILL match / in CI <= 2.1.3
So to make it compatible for both versions we will just manually put in the character match without using CI's shorthand syntax by using [^/]+ in place of :any. Note we do not put in parentheses as we do not want to create a backreference.
Also you do not put ^ as CI automatically puts it in. In addition, you have your backreferences wrong. You don't use $0, you start with $1 and work up. I changed them to correct numbers.
Your route should be this:
$route['((?!login|profile|signup|notification|member)|member[^/]+)/(:num)/(:any)/(:any)'] = "home/$3/$1/$2/$4";
So for the following URL example:
memberfolder/7/view_document/WjbZpAPHM6
The backreferences will be:
$1 = memberfolder
$2 = 7
$3 = view_document
$4 = WjbZpAPHM6
Now to build your request:
home/$3/$1/$2/$4 becomes home/view_document/memberfolder/7/WjbZpAPHM6
Making the following rsegments:
rsegment(1) = home
rsegment(2) = view_document
rsegment(3) = memberfolder
rsegment(4) = 7
rsegment(5) = WjbZpAPHM6
I don't know much about CodeIgniter, but I think, you should replace
$route['^(?!login|profile|signup|notification|member).*/(:num)/(:any)/(:any)'] = "home/$2/$0/$1/$3";
with this
$route['^(?!login|profile|signup|notification|member).*/(:num)/(:any)/(:any)'] = 'home/$2/$0/$1/$3';

rewrite url and let more parameters

I am rewriting this kind of urls
http://deia.info/category/calendar/10/
To
/index.php?task=browse_posts&catid=$2
By this code in the .htaccess
RewriteRule ^category/(.+)/(.+) /index.php?task=browse_posts&catid=$2
Which seems to do the job. The problem is when additional parameters are sent, those are ignored.
For example:
http://deia.info/category/calendar/10/?start_date=10
isset($_GET['start_date']) won't return true
Why?
When the incoming URL holds a query like in the OP example:
http://deia.info/category/calendar/10/?start_date=10
The way to combine (Append) it with/to a new query in the substitution URL, is by adding the QSA Flag.
So add [L,QSA], like this:
RewriteRule ^category/(.+)/(.+) /index.php?task=browse_posts&catid=$2 [L,QSA]

URL routing/rewriting and compatibility with Nginx/Lighttpd

I'm writing a simple URL routing code based on using mod_rewrite to pass the URI as a GET parameter, like Drupal does. So I have the rule:
RewriteRule ^(.*)$ index.php?q=$1 [QSA,L]
And the URL http://www.example.com/test/1 would give me "/test/1/" passed as value $_GET['q'], instead of the usual index.php/test/1 and having to extract that from $_SERVER['REQUEST_URI'].
The thing is, the mod_rewrite QSA flag allows me to still use query strings normally, which I find very useful for parameters like filters and pagination, like "/products/category/?pg=1".
Will this work the same on Nginx and Lighttpd servers? I'd like my code to be portable.
Thanks.
No, in either case you'll have to translate your rules into each server's specific syntax. Some links to get you started:
http://forums.digitalpoint.com/showthread.php?t=187965
http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModRewrite
https://serverfault.com/questions/24243/nginx-support-for-htaccess-rewrite-rules-differences-from-apache

Categories