php request vars in sef urls - php

I have a page (ie www.mysite.com/products.php?catid=5) in which I use the $_REQUEST[catid'] to get the category id and use it in a query. I switched to SEF urls and now the urls display like www.mysite.com/products/category/5
How can I use the new urls to retrieve the catid value?
The following lines were used in the .htaccess file for switching to SEF urls:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]

You need to rewrite the URL correspondingly using mod_rewrite for example.
RewriteRule ^(.*)$ index.php?url=$1 [NC,QSA]
This rule would rewrite the URL www.mysite.com/products/category/5 to www.mysite.com/index.php?url=products/category/5. From this point on you can parse the variable $_GET["url"] or refine the rewrite rule.

Since the htaccess takes effect before the PHP starts to build you can grab the current URL with the below code snippet and get the value of the element as follows using the literal example of
www.mysite.com/products/category/5
$currentURL = $_SERVER['REQUEST_URI'];
$elements = explode("/",$currentURL);
$id_variable = $elements[3];
if you include the http:// in the url, then the element count should be 5 I believe
OR ... if you know that the id will always be the last element
$id_variable = $elements[(count($elements)-1)];
that will grab the last element in the array;
you can always use the following to show the data temporarily
echo "<div style='background-color:#FFF;'>";
echo ("Elements array:<br>\n");
print_r($elements);
echo "</div>";
hope this helps

Related

htaccess for load subdomain with parameters

I would like to know how I can get a second parameter when I load data from an htaccess, I have everything working so that I get the first parameter that is the subdomain, the second parameter would be the "GET" that it gets from the URL, for example:
I want to load:
subdomain.domain.com/category?idx=22
With the following code I get the first parameter that is the subdomain but I can't get the idx after category
This is the code:
RewriteCond %{HTTP_HOST} !^(www\.)?domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com$ [NC]
RewriteCond "%{REQUEST_URI} /%1" !^(/[^/]+)[^\s]*\s\1$
RewriteRule ^category$ loader-category.php?&id=%1&idx=$2 [L]
what's the correct regex for this?
I appreciate any help
RewriteRule ^category$ loader-category.php?&id=%1&idx=$2 [L]
What would $2 be referring to here? You don’t have any capturing groups in the pattern.
Use the QSA flag here, so that the original query string gets appended / merged with the new one you are creating.
https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_qsa
RewriteRule ^category$ loader-category.php?id=%1 [L,QSA]

Rewrite Dynamic URL In PHP Using .htaccess

I wanted to rewrite url in following pattern
My current url is:
xyz.com/news_details.php?id=120&title=hello-world
and I want the url as is:
xyz.com/hello-world
also I wanted to fetch the values of id on this page like:
$id = $_REQUEST['id'];
please help
I did use the following code:
RewriteCond %{THE_REQUEST} /news_details.php\?id=([^&\s]) [NC]
RewriteRule ^ /%2\.html? [NC,R,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^-])\.html$ /news_details.php?id=$2 [QSA,L,NC]
You could do something like this :
RewriteEngine On
RewriteRule ^/(.*)/(.*)$ /news_details.php?title=$1&id=$2
To achieve links like http://example.net/hello-world/120.
The ^/(.*)/(.*)$ part tells apache the pattern that it will follow. The second part (news_details.php?title=$1&id=$2) gives apache the real link that it will apply the pattern on.
You can't exactly hide arguments from the user, because you need a way to get the id that you want to have as the id argument. So that's the only possible way to do that.
You can read more about it here.

Secure url parameter

ok,Let me explain you step by step,Point is im new for this developments.
php files in my cproject directory :
allproducts.php
watch.php
in allproducts.php im displaying all the products available in the database. and every products has it own url. In my watch.php im displaying certain data according to a parameter comes from the allproducts.php page.
in my allproducts.php page i have this url :
http://localhost/cproject/watch?v=
url looks like something like this :
localhost/cproject/watch?v=J46TKlqSw3Gt4sk
at the moment i wrote a rewriterule for make watch.php in to "watch"
now what i want is to get rid of this "?" mark in to "/" and "v= " into "/"
so i want my rul to looks like this
localhost/cproject/watch/J46TKlqSw3Gt4sk
i hope now u do understand what i want to do actually ?
if i suddenly explain you what i have on my .htaccess file for this watch.php
i have this line of code
RewriteRule ^watch watch.php [NC,L]
You have to use .htaccess for doing url this.
.htaccess file code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
For PHP version less than 5.2.6 try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
If you need to get the parameter from the url then use:
javascript
var str = "http://localhost/cproject/watch?v=J46TKlqSw3Gt4sk";
var param = str.split("=")[1];
alert(param);
Result
J46TKlqSw3Gt4sk
With the new url
var str = "http://localhost/cproject/J46TKlqSw3Gt4sk";
var param = str.split("/");
alert(param[param.length - 1]);
Result
J46TKlqSw3Gt4sk
For the .htaccess file you can use this RewriteRule which will forward the urls with just a / to the url with /watch?v=.
RewriteRule ^cproject/(.*) cproject/watch?v=$1 [NC]
For example:
Start URL http://localhost/cproject/eeeeeeeaf
End URL http://localhost/cproject/watch?v=eeeeeeeaf
But what i want is to get a url like i have mentioned above and get the parameter in a different page
You can do this with PHP from the /watch file by using the REQUEST_URI element in the $_SERVER[] array.
For the PHP code you could do:
<?php
$request = $_SERVER['REQUEST_URI'];
$exploded = explode("/", $request);
$id = $exploded[4];
echo $id;
?>
In regards to your additions to your question, it is easy to do as I have put above. Instead of what I have put, you can instead just use:
RewriteRule ^watch/(.*) watch.php?v=$1 [NC,L]
You can add this rule alongside your current rule (preferably after) and it should function as you require.
now what i want is to get rid of this "?" mark in to "/" and "v= " into "/" so i want my rul to looks like this localhost/cproject/watch/J46TKlqSw3Gt4sk
The URL RewriteRule I provided will do this :)
Have you actually tested the rewrites you have been provided with?

How do I turn PHP string into slug in Wordpress

I have a page in wordpress I am using with a slug called Bad-Debt-Recovery-in/. I am using a custom php query on that page with strings in the URL's like this
Bad-Debt-Recovery-in/?zipcode=55555&location=Chambers%20County+AL
How can I make this url into a slug like this
Bad-Debt-Recovery-in/55555/Chambers-County/AL/
as a rewrite? Any help would be appreciated!
UPDATE:
This code is actually what I am using. I also made it simpler and created a third variable named "state". One rewrite is for City and one is for County page:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^Bad-Debt-Recovery-And-Collection-Agencies-Services-In\/([^\/]+)\/([^\/]+)\/([^\/]+)\/? Bad-Debt-Recovery-And-Collection-Agencies-Services-In/?zipcode=$1&city=$2&state=$3 [QSA,L,NC]
RewriteRule ^Bad-Debt-Recovery-And-Collection-Agency-Services-In\/([^\/]+)\/([^\/]+)\/([^\/]+)\/? Bad-Debt-Recovery-And-Collection-Agency-Services-In/?countyid=$1&county=$2&state=$3 [QSA,L,NC]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
</IfModule>
# END WordPress
What you are asking for, and seeking to accomplish is called: Converting a request path into a query string.
Use .htaccess RewriteRule directive to modify the incoming request
RewriteRule ^Bad-Debt-Recovery-in\/([^\/]+)\/([^\/]+)\/([^\/]+)\/? Bad-Debt-Recovery-in/?a=$1&b=$2&c=$3 [QSA,L,NC]
Each ([^\/]+) captures path elements into a variables $1,$2,$3...
The ? at the end simply denotes that the last / is optional or else the last match could fail
the \ simply escape the '/' for literal interpretation
Add as many ([^\/]+) as needed and capture them in the query
zipcode=$1&location=$2+$3
The modifiers at the end [QSA,L,NC] are called flags
QSA appends the query string to the end of the rewrite if any exist
L simply says this is the last rewrite
NC means not case sensitive
This is your final solution:
RewriteRule ^Bad-Debt-Recovery-in\/([^\/]+)\/([^\/]+)\/([^\/]+)\/? Bad-Debt-Recovery-in/?zipcode=$1&location=$2+$3 [QSA,L,NC]
You can use the WP function sanitize_title
Combine this with a php loop i.e.
$url = $_SERVER['REQUEST_URI'];
foreach($_GET as $g){
$url .= '/'.$g;
}
$url = sanitize_title($url);

htaccess and php - redirects and pretty urls

I have a webcommunity, and it's growing now. I like to do a link makeover for my web, and then I need to know the best solution for my case.
Right now my htaccess looks kind of like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?page=user&username=$1 [L]
You are able to link to users like this domain.com/username and that's nice.
Then I have different pages like
index.php?page=forum&id=1
index.php?page=someotherpage&id=1&anotherid=5
index.php?page=3rd
... and so on. I want them to look something like this:
domain.com/forum/23/title-of-the-thread
domain.com/page2/id1/id2
... and so on.
How do I make these pretty urls without removing my domain.com/username functionality? What solution would you suggest?
I was thinking about creating a file that checks the URL, if it matches any pages, and users and so on. Then it will redirect with a header location.
If all of the urls you are going to rewrite are going to the same end point, you could simply use:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
in index.php:
<?php
$url = $_SERVER['REQUEST_URI'];
How you use the request uri is up to you, you could for example use a simple strpos check:
<?php
$url = $_SERVER['REQUEST_URI'];
$rules = array(
'/forum/' => 'forum',
'/foo/' => 'foo',
'/' => 'username'
);
foreach($rules as $pattern => $action) {
if (strpos($url, $pattern) === 0) {
// use action
$file = "app/$action.php";
require $file;
exit;
}
}
// error handling - 404 no route found
I was thinking about creating a file that checks the URL,
you actually have that file, it's index.php
if it matches any pages, and users and so on. Then it will redirect with a header location.
that's wrong. HTTP redirect won't make your URLs look "pretty"
you have to include appropriate file, not redirect to.
Just change your rule to more general one
RewriteRule ^(.*)$ index.php [L,QSA]
You basically have two options.
Route all URLs to a central dispatcher (FrontController) and have that PHP script anaylze the URL and include the correct scripts
Note every possible route (url rewrite) you have in the .htaccess
I've always worked with option 1, as this allows greatest flexibility with lowest mod_rewrite overhead. Option 2 may look something like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^forum/([^/]+)/([^/]+)/?$ index.php?page=forum&id=$1 [L]
RewriteRule ^otherpage/([^/]+)/([^/]+)/?$ index.php?page=someotherpage&id=$1&anotherid=$21 [L]
RewriteRule ^page/([^/]+)/?$ index.php?page=$1 [L]
# …
RewriteRule ^([^/\.]+)/?$ index.php?page=user&username=$1 [L]
you said
I was thinking about creating a file that checks the URL, if it
matches any pages, and users and so on. Then it will redirect with a
header location.
While "creating a file that checks the URL" sounds a lot like option 1, "redirect with a header location" is the worst you could do. That would result in
an extra HTTP roundtrip for the client, leading to slower page loads
the "pretty URL" won't stick, the browser will show the URL you've redirected to
losing link-juice (SEO)
This can be done entirely with htaccess or php
//First Parameer
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?page=$1
//Second Parameter
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ index.php?page=$1&username=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ index.php?page=$1&username=$2
read more about it here:
http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/
http://www.roscripts.com/Pretty_URLs_-_a_guide_to_URL_rewriting-168.html

Categories