convert dynamic to seo friendly url - apache

I know there is plenty on this issue in SO and elsewhere, and I read it all by now, but I do not seem to get this to work despite sweating it out all day.
So, see if you guys can point me in the right direction.
I have a dynamically created url ( by customer selecting a form at index.php ) that looks like, as an instance:
http://www.example.com/marques.php?brand_id=39-Ford
I need it to show to the user and search engines as:
www.example.com/marques/Ford
I have set up .htaccess as follows:
...other .htaccess stuff..
RewriteEngine on
RewriteCond %{REQUEST_URI} ^\/marques #here, I believe, to apply this only when URI is /marques
RewriteRule /([A-Z][a-z]+$)/ marques/$1 [L] #here I meant to get 'Ford' in this instance.
I plan to have the destination file, marques.php handle the request with the given brand info.
This is not working at all. I continue to see the 'old' URL.
Of course, the next step would be to have the .htaccess redirect when the customer clicks on the 'friendly' link at the search engine ( hopefully soon ). But I need the first step to be resolved.
By the way, I checked with my provider and they confirm that mod_rewrite is enabled.
Thanks for your help.

You can use this your root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+marques\.php\?brand_id=([^\s&]+) [NC]
RewriteRule ^ /marques/%1? [R=301,L]
RewriteRule ^marques/([^/.]+)/?$ marques.php?brand_id=$1 [L,QSA,NC]

After further investigation I realized I fell for this widespread misconception that one can use mod_rewrite to create SEO friendly URL's. Unfortunately, this misconception is reinforced throughout SO as well.
After reading this article from Matthew James Taylor and this it became clear that one has to create the SEO friendly URL elsewhere, not through mod_rewrite, as was my thinking.
Mod_rewrite is ONLY part of the process.
If one has only links with anchor tags, one has to go in each page of the website and hardcode the change to the SEO friendly URL. In my case, the URL's are created dynamically.
May be there are alternative solutions, but this did the trick.
So, first I changed my form, from this:
<form action="marques.php " method="post" class="form1" >
<select class="select_home" name="brand_id" onchange="form.submit()" >
<option value= "<?php echo $row['brand_id']; ?>">"<?php echo $row['brand']; ?>"</option>
To this new set up:
<form action=" " method="post" class="form1" >
<select class="select_home" name="brand" onchange="form.submit()" >
<option value= "<?php echo $row['brand']; ?>">"<?php echo $row['brand']; ?>"</option>
What I am doing above is submitting the form to the page itself by placing action = " " and changing name and value to 'brand' ie, the name of the brand as opposed to the brand_id. This last one is meant to have the URL 'really' friendly. One can decide to have both the name and the id in the URL.
The SEO friendly URL is 'created' in the sequence. This script is placed at the very top of the same page, before anything else, otherwise 'header' will not work.
if(isset($_POST['brand'])){
$brand = $_POST['brand'];
$brand = str_replace(" ", "-", $brand);
$url = '/marques-voiture/' . $brand;
header("Location:" .$url );
exit();
}
So, in the above the SEO friendly URL is created and will show at the browser:
www.example.com/marques/Ford or www.example.com/Land-Rover
Next, now yes, with mod_rewrite tell the browser how to make sense of that URL and fetch the right script which is:
www.example.com/marques.php?brand=Ford
I made that as follows within .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/?marques-voiture [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([\-A-Za-z]+$) /marques.php?brand=$1 [L]
Of course it is necessary to change the destination script to receive a GET, instead of a POST which now contains the brand and not the brand_id as before.
Hope that helps.

Related

Rewriting directory into a query parameter in htaccess

I converted a website I'm building into a web view app in iOs.
I would like to track visitors that use the app instead of the website by adding a directory to my URL.
For instance, the "about" page URL would go from "https://example.com/about/" to "https://example.com/app-ios/about/"
My question is how to write an htaccess rule that tells my server to go to the path "/about/" and skip the "/app-ios/" directory?
Also, I'd like to add ?app=app-ios in my query parameters.
The most promising thing I found was this :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search/node/(.+)$ /search/node/?app=$1 [QSA,NC,L]
But I'd need to specify what comes before "foo".. In my case, "app-ios" is at the beginning of the request uri, always.
Plus I don't want a redirection. I just want my server to read /app-ios/something/other-thing/ as /something/other-thing/?app=ios.
First one question for tracking I would recommend to make this over a Query Parameter (as you wrote) like ?client=ios, but this might be an own opinion.
For rewriting the URL you could do the following:
to remove from the IOS from the URL (not tested):
RewriteEngine On
RewriteRule (.*)ios/(.*) $1/$2?client_id=ios&%{QUERY_STRING} [L]

How to set the right conditions in htaccess file?

I've tried for ages to fix this problem that I have.
On my website I have rewriten some links with the htaccess file. The htaccess file currently looks like this:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?sideID=$1
RewriteRule ^([a-zA-Z0-9_!-]+)/([a-zA-Z0-9_!-]+)/([a-zA-Z0-9_!-]+)$ index.php?sideID=$1&id=$2&title=$3
RewriteCond %{HTTP_HOST} ^www\.mypage\.no$
RewriteRule ^/?$ "http\:\/\/mypage\.no\/" [R=301,L]
The first rule rewrites from:
http://mypage.com/index.php?sideID=home
To this:
http://mypage.com/home
The first rule rewrites from:
http://mypage.com/index.php?sideID=b&id=12&title=a-great-blog-post
To this:
http://mypage.com/b/12/a-great-blog-post
Soo they do what I want too when it comes to rewriting I guess. BUT, the problem is that if im now navigated to any blog post on the site and then tryies to navigate back to whatever other link lets say http://mypage.com/home it will add http://mypage.com/b/12/home instead. That can of course be fixed easily by just using absolute URL's in the navigation. But when I try to make a XML Sitemap, the bot will index every page for each blog ID like so: http://mypage.com/b/12/home, http://mypage.com/b/13/home, http://mypage.com/b/14/home and so on for each blog ID and pagename.
Is there a way to make sure the RewriteRule's apply seperatly or set a condition for them or something like that?? I am a bit noob in this area.
I beg you, please help me! :)

URL rewriting and redirection

I just finished my rewriting with .htaccess and everything works just fine.
RewriteEngine on
RewriteRule blog/([a-zA-Z0-9\-]+)-([0-9]+) post.php?url=1&id=$2
The only I want to avoid is the duplicate content... I've searched a lot on the subject and sadly found anything matches my needs.
The fact is, the rewrited address "blog/my-new-post-77" is also accessible by "post.php?id=77" and I don't want it to happen. So I would like to redirect every post.php pages to the rewrited rule.
Someone have an idea for me?
Yes, add extra variable to your rewrite rule to check it:
RewriteRule blog/([a-zA-Z0-9\-]+)-([0-9]+) post.php?check=ok&url=$1&id=$2
And at the top of your post.php file, check for that check variable:
if(isset($_GET['check'])){
if($_GET['check'] == "ok"){
//it comes using rewrite rule
//cut and paste all of your codes in post.php file to here
//that is which codes are available when user view your page in desired way
//don't redirect to rewrite rule again here, since visitor came using that rewrite rule to here. doing so will result infinite redirect and will show an error
}else{
//this visit is not used rewrite rule to come here
//here you can redirect visitor to not found page or just show an empty page by putting die();
//you can't redirect to post pages here, because you don't know what post(id) that visitor need to see
}
}else{
//this visit is not used rewrite rule to come here
//here you can redirect visitor to not found page or just show an empty page by putting die();
//you can't redirect to post pages here, because you don't know what post(id) that visitor need to see
}
post.php?id=77 -> /my-new-post-77 via the browser
/my-new-post-77 -> post.php?id=77 via internal rewrite
Is it right?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule blog/([a-zA-Z0-9\-]+)\-([0-9]+)$ post.php?url=1&id=$2 [L]
RewriteCond %{THE_REQUEST} \s\/post\.php\?id\=(\d+)\s
RewriteRule . /my-new-post-%1 [R,L]
I found a solution which matches my needs waaaay more.
if($data["url"]!=$_GET["url"]) {
header("Location:http://www.mywebsite.com/blog/".$data["url"]."-".$data["id"]);
}
This solution forces the post.php?id=XX to go to the rewrited location as we wanted and by the same time, encounter any manual url rewriting.
$result being the SELECT ALL of my databases rows.
$data = mysqli_fetch_array($result);
I tested #Janaka solution, with your great explanations, and it worked tho.
Thanks everybody; Case closed :)

Need to remove certain URL paths from URL htaccess

Ok I'm new to ht access and i have been scouring the web for an answer on this. I currently am using the Interspire shopping cart and the add certain paths in the urls, for example: categories, products, pages and brands before the real SEO part of the url begins. It sucks. I'm looking for a way to get rid of this in for of a mod_rewite in htaccess. Here are some more visual examples:
Current: cases.com/categories/Amazon-Kindle-Cases/
Want it to be this: cases.com/Amazon-Kindle-Cases/
Current: cases.com/products/Piel-Frama-559-iMagnum-Black-Leather-Case-for-Amazon-Kindle-Fire.html
Want it to be this: cases.com/Piel-Frama-559-iMagnum-Black-Leather-Case-for-Amazon-Kindle-Fire.html
Current: cases.com/brands/PDair.html
Want it to be: cases.com/PDair.html
Current: cases.com/pages/News.html
Want it to be: cases.com/News.html
Do I need a rewrite for each one or can this be done in one. Thanks
RewriteCond $1 ^(products|brands|pages|categories)
RewriteRule ^(products|brands|pages)(.*)$ $2 [L]
Is that working?
more specifically:
RewriteEngine on
RewriteCond $1 ^(products|brands|pages|categories)
RewriteRule ^(products|brands|pages)(.*)$ http://cases.com$2 [L]

Proper mod_rewrite for SEO friendly URLs

I have a custom php script, each having a page for page.php?id=[number] and page.php. I've read a few .htaccess tutorials but I don't know which one is the most appropriate way of doing it:
Example,
page.php?id=12 to page/id-12/and
page.php to page/ AND page
Here's what I currently have:
RewriteEngine On
RewriteRule ^category/([0-9])$ /category.php?id=$1
RewriteRule ^category/$ /category.php
Code above returns 404 not found if i access category but appears okay if i access category/ (with a slash)
All your rules include the slash / at the end of category, thus of course category cannot match it.
RewriteRule ^category/?$ /category.php
The ? after / makes it optional