Can anybody advise how I can rewrite the following URL:
www.mydomain.com/products.php?product=product-name
At the moment, it works fine (I use $_GET to grab the unique product name and use it on my page) but I would like to be able to use the following URL format to get the same outcome:
www.mydomain.com/products/product-name
I've seen a few similar examples on here but cannot get them to work with my situation.
This is how your .htaccess will look like,
RewriteEngine On
RewriteRule ^products/([A-Za-z0-9-]+) /products.php?product=$1 [NC,L]
RewriteEngine On this is used to turn on the rewrite engine.
^products/([A-Za-z0-9-]+) matches the URL like (www.mydomain.com/products.php?product=product-name) where product name can be one or more of any combination of letters, numbers and hyphens.
And use that combination of letters in /products.php?product=$1 where $1 denotes the Product name.
Related
htaccess file with the following:
RewriteRule ^home?$ index.php
This works great for rewriting the url of my home page. Users are able to post articles on my site so the url for all the various pages are as follow:
http://example.com/article.php?id=22
Let's pretend there are 1'000 articles how can I rewrite each article to something like this:
example.com/articleId/articleTitle
For example:
example.com/56732/How-to-bake-bread
Is it possible to extract the data of an article and use it to rewrite the article's URL?
I guess you're looking for that:
RewriteRule ^([0-9]+)/(.+)?$ article.php?id=$1 [NC,L]
The first part of the regex ([0-9]+) captures the id in the url and is passed in the value passed in $1, if you wanted to use as well the (.+) part you could get it using the value in $2.
I need to build SEO friendly URLs, I have this URL:
http://www.misite.com?search=&area=$1&cityid=$1&lang=es&subcatid=66&view=ads&catid=9
I need to make them look like this SEO friendly URL:
http://www.misite.com/sale-houses-california
So far, I've added this code to htaccess this:
RewriteRule ^sale-houses-([-]?[a-z]+)? ?search=&area=$1&cityid=$1&lang=es&subcatid=66&view=ads&catid=9 [QSA]
I have cities, categories and subcategories combining all so I need an easy solution for all of the combinations
I don't know the correct expression: ([-]?[0-9]+)? / ([-]?[a-z]+) or similar.
The URL structure is ^text-text-text in lower case
Is it possible to make something more general like this (see below) or do I have to write a line per each category?
RewriteRule ^([-]?[a-z]+)-([-]?[a-z]+)-([-]?[a-z]+)? ?search=&área=$1&cityid=$1&lang=es&subcatid=$1&view=ads&catid=$1 [QSA]
Lets say I want users to be able to type this url in:
www.website.com/blog/2453/I-gained-0.1%-more-scripting-knowledge-!
I'm trying to include title information in the url for seo benefits.
I also want to include an id for my query. Effectively I want to pick up the id and ignore the title stuff that comes after, bearing in mind its user generated text so could contain any special characters in it.
How can I write a .htaccess rewrite rule so that the server reads it as the following with the appropriate GET data:
www.website.com/blog.php?id=2453
This is what I have tried but frankly I am way out of my depth here:
RewriteRule ^blog/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ blog.php?id=$1 [NC,L]
The rewrite rule you are using should work except for the ., %, and ! characters that are in your URL. The % characters is not safe to use in URLs because it has a special meaning in the URL syntax. I wouldn't use exclamation points either.
If the ID is always going to be numeric, use ([0-9]+) instead of ([A-Za-z0-9-]+).
Try this URL:
www.website.com/blog/2453/I-gained-0.1-more-scripting-knowledge
With this rule:
RewriteRule ^blog/([0-9]+)/[A-Za-z0-9\-\.]+/?$ blog.php?id=$1 [NC,L]
I have a website which refers to to various Indian cities. Code makes links from an external source, which may name a city as Calcutta, calcuta, Kolkata, etc.
I need a htaccess 301 redirect rule which, given all the misspellings would redirect the following the known good spelling (Kolkata):
Calcutta-to-Delhi.html to Kolkata-to-Delhi.html
and also
Calcuta-Airport.html to Kolkata-Airport.html
Thanks.
What you're asking for is not simple and need you to have a powerful computer, but the results are simply amazing.
Here's what I'd suggest to do:
You have to test if your expression matches in the URL / i.e. http://mysite.com/(expr1)-to-(expr2)/
If yes and there's no file, redirect to a Php file that handles everything.
In this Php file, analyze the URL (once again): if your expression matches /(expr1)-to-(expr2)/ then do a SOUNDEX search with MySQL (in your Php file). See query sample here.
Then, in this "special" 404 case, do a suggestion, like google does, i.e.: "did you mean Kolkata-to-Delhi.html? if so, click on the link".
This a hard work, but it's both interesting and shows your skill. Very few websites do this (I just know google actually).
Maybe you can try something like this:
# REDIRECTS FROM (eg): http://www.calcutta.com.au/ to http://www.kolkata.com.au/
RewriteCond %{HTTP_HOST} ^calcutta\.com\.au$ [nocase, ornext]
RewriteCond %{HTTP_HOST} ^www\.calcutta\.com\.au$ [nocase]
RewriteRule ^(.*)$ http://www.kolkata.com.au/ [nocase,L,R=301,O]
Notice the "[nocase]" which ignores case. This will only redirect from "calcutta" to "kolkata" and not from "calcuta" or "Kolkata" (notice the missing 't' in calcuta and uppercase 'K' in Kolkata).
You will need to add two more rules following the same pattern of code as above.
I'm creating a frontpage for my website with a single form and input text, Google-style. It's working fine, however, I want to generate a pretty URL based on the input. Let's say, my input is called "id", and using the GET method of form, and the action defined to "/go/", on submission, the URL will be:
site.com/go/?id=whateverIType
and I want to change it to
site.com/go/whateverIType
I was thinking on Mod Rewrite, but if the user put something in the URL, like:
site.com/go/?dontwant=this&id=whateverIType&somemore=trash
I want to ignore the other variables but "id", and rewrite the rule.
What's the better way of get this done? Thanks in advance!
PS: I'm using CodeIgniter, maybe there's something I can use for it as well. I already have a controller for "go".
I'm not familiar with CodeIgniter, but you can try the following RewriteRule
RewriteEngine on
RewriteCond %{REQUEST_URI} ^\/go\/
RewriteCond %{QUERY_STRING} id=([^&]*)
RewriteRule (.*) /go/%1? [L,R]
The %1 references the regex group from the previous RewriteCond, and the trailing ? will strip the querystring from the redirected URL.
Hope this helps.
Mod_rewrite supports conditions and rules with RegEx, so you could have a rule that matched the ?id=XXXX, that would extract it from the URL (keeping the other parameters), and rewrote the URL accordingly.
However... I don't think you want to do this, because if you rewrite the URL to be /go/Some+Search+Query, you won't be able to pick it up with say, PHP, without parsing the URL out manually.
It's really tough to have custom, SEO-friendly URLs with user input, but it is technically possible. You're better off leaving in the ?id=XXX part, and instead, using mod_rewrite in the opposite approach... take all URLs that match the pattern /go/My+Search+Terms and translate that back into something like ?id=My+Search+Terms, that way you'll be able to easily parse out the value using the URL's GET parameters. This isn't an uncommon practice - Google actually still uses URL parameters for user input (example URL: http://www.google.com/search?q=test).
Just keep in mind that mod_rewrite rewrites the URL before anything else (even PHP), so anything you do to the URL you need to handle. Think of mod_rewrite as a regular expression-based, global "Find and Replace" for URLs, every time a page is called on the server. For example, if you remove the query string, you need to make sure your website/application/whatever accounts for that.
In application/config/routes.php
$route['go/(:any)'] = "go/index/$1";
Where go is your controller and index is the index action.
http://codeigniter.com/user_guide/general/routing.html
You can use something like this in your .htaccess if you aren't already:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]