RewriteRule from download.php?token=something into /download/something - .htaccess

I am working on url, I try htaccess, php, javascript and many other things but unable to figure it out.
My url is :
example/cheap-flight-to.php?country=lagos
and I want to change the url something like this :
example/cheap-flight-to.lagos
or
example/cheap-flight-to/lagos
please help me

The following should allow you to generate your urls in the format that you wish.
RewriteEngine On
RewriteBase /
RewriteRule ^example/cheap-flight-to/([a-zA-Z]+)$ /example/cheap-flight-to.php?country=$1 [NC,L]

What you want could be done using regular expressions in .htaccess, but it makes no sence, since it wouldn't be pointing to anything, unless you have a directory cheap-flight-to/lago in which you have an index.php that will show-up in the browser or return data. This means you have to setup a directory for each destination you want to go to. Is that really what you want? Usually it's been used the otherway around. The user (or a script for that matter) enters a url like "example/cheap-flight-to/lagos". Then through regular expressions in .htaccess you rewrite the url to "example/cheap-flight-to.php?country=lagos".
Have a look at http://httpd.apache.org/docs/1.3/misc/rewriteguide.html and
http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html for more on rewriting rules in .htaccess.

Related

.htaccess rewrite url that has already been changed

I am upgrading my site which involves new scripts and a different URL
structure. There are currently a few thousand pages so I want to
basically move them in to a subdirectory so that they are not lost.
I am not very confident with htaccess so can someone please confirm that
the first part I have done is correct:
Old URL: http://www.example.com/article/another-dir/page-group/whatever.html
RewriteRule ^article/?$ http://www.example.com/archive/ [R=301,NC,L]
To achieve this: http://www.example.com/archive/another-dir/page-group/whatever.html
Search engines will see the above as a permanent move and in the address bar
it will show the new url. Is this right?
The second part is on the new script - the url's could be improved but I am
unable to change all the script so thought about using htaccess again but am
not sure if it can be achieved like this.
At the moment the url looks like this:
url: http://www.example.com/category/4/categoryname
In the htaccess the current rewrite rule for this type of url looks like this:
RewriteRule ^category/(.*)/(.*)$ category.php?id=$1&slug=$2
Is it possible to change this so that in the url address bar I end up
with this:
http://www.example.com/categoryname
Basically I don't want to see either the number or category/ in the resulting
url as it will be easier for visitors to use - is that possible??
Thanks in advance for any help.
The second question related to passing in URI components for querystring redirect and then hiding those components in the URL I don't think would be easy, if even possible, using RewriteRules.
For the first question though. Given the sample URLs you mentioned, the RewriteRule would need to include capture and backreference if you want to preserve the full URL in the redirection. For example:
RewriteRule ^article/?(.*)$ http://www.example.com/archive/$1 [R=301,NC,L]

Masking file name with htaccess

I have a client that has several files whose name is (for example) car.php, car_edit.php, car_review.php. These each come with query strings - so car.php?id=1234 or car_review.php?id=321. They would like the file names to be truck*.php rather than car*.php.
I'm hoping there's a way using htaccess to convert the url string to be truck*.php and use the current car*.php files. Also if possible I'd like to forward any page asking for car*.php to truck*.php.
I've done quite a bit of searching and haven't found an answer to doing this particular thing. Does anyone know how I might do this? Thanks.
You need rewrite rules. Try something like:
RewriteRule ^truck(.*).php$ /car.php?id=$1 [NC,L]
Note: This is untested, so may require tweaking.
RewriteEngine On
RewriteRule ^truck(.*)\.php$ /car$1.php [NC]
ought to do it. It should automatically transfer any URL query string like id=xxx over to the car*.php rewritten URL.

.htaccess rewriting certain word in url

I cannot seem to find the answer to this
I am looking for a .htaccess rewrite way to turn this url
http://www.mydomain.com/Virtuemart.html?keyword=adobe&page=shop.browse
to
http://www.mydomain.com/virtuemart.html?keyword=adobe&page=shop.browse
So basically i just want to change the capital V
There are many pages and variables after the ? so doing it one by one is almost impossible
is their an easy way to do this.
Many thanks for your time
David
Using mod_rewrite (under apache), you can stick something like this in the .htaccess file in your document root:
RewriteEngine On
RewriteRule ^Virtuemart.html /virtuemart.html [L,QSA]
The query string just gets appended to the end untouched. If you want to redirect the browser, add a R=301 flag to the end (or just R if you don't want 301). This makes it so when someone goes to http://www.mydomain.com/Virtuemart.html, they'd actually get served the page at /virtuemart.html.

htaccess removing index.php and search queries from URL

I have a site that, for a certain php function to work, needs the url to be:
/topic/index.php?height=###
I would like the URL to read
/topic/
What can I put in the .htaccess file to achieve this? Can I put one htaccess file in the root, or would I need to put one in each /topic/ directory?
I think the following might work for your issue:
RewriteRule ^([^/]*)/?$ $1/index.php?height=###
Of course, that's assuming a static number. If you need a dynamic number or one provided by the client, you're going to need something like:
RewriteRule ^([^/]*)/(\d+)/?$ $1/index.php?height=$2

URL Rewriting based on form input

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]

Resources