mod_rewrite for name fields - .htaccess

I am complete newbie in mod_rewrite, and I have been going through some sites reading how to handle this, all I could find was when the get variable was numbers and nothing about strings, and it turns out be over my head.
What I want to do is to rewrite display.php?name=blahblah123 to display/blahblah123
Together with the answer, I would love some sites where I can build some grasp over mod_rewrite myself.
Thanks
Edit:
With more searching, I came up with this
RewriteEngine On
RewriteBase /
RewriteRule ^display/([a-z]+)$ display.php?name=$1 [L]
The code above works, but for some reason, The page I get has no CSS. The CSS I see included shows Hostgator's 404 page CSS. But HTML looks fine, so does the content and everything else. Any idea?

Just to be clear, mod_rewrite is something that happens internally in apache. I find that sometimes people do not understand what it does and doesn't do. It will not take a url that you output and change it from
display.php?name=blahblah123
to
display/blahblah123
It will however, allow someone to make a request from your site for display/blahblah123 and convert that to display.php?name=blahblah123, so that your display.php script can operate on it.
Rewrite rules require an understanding of regular expressions. The better you understand regex the easier mod_rewrite will be for you.
As a beginner this article should help: http://www.sitepoint.com/guide-url-rewriting/
For your specific question, I'd probably use:
RewriteRule ^display/([a-zA-Z0-9]+)$ /display.php?name=$1

Try to put a "/" in front of the css file name.
<link rel=".." href="/style.css".
instead of
<link rel=".." href="style.css".

Related

.htaccess redirect to another page while retaining the complete query string

I am a little lost. I tried searching this site and the web at large, but I couldn't seem to find exactly what I need. So please forgive me if this has been answered before, I really tried to find it.
I have … inherited a .htaccess file with quite a lot of 301 redirects in the
Redirect 301 /shorthand /actual-link/actual-file.php
syntax (with a RewriteEngine On line somewhere high up in the file). I don't know exactly much about redirects, but that seems pretty straightforward to me. It just sits there and sometimes new shorthand links get added. There is some non-www to www, and http to https kind of stuff at the top, that's it.
Now the structure of the site changes, and two similar pages that process query parameters get consolidated into one. Basically there is
Old page: /path/subpath/index.php?some=query&parameter=or&other
New page: /other-path/file.php?added=parameter&some=query&parameter=or&other
I can't predict what parameters exactly will be part of the URL, I just have to take everything starting from the ? and append it to the new URL, that already has to include an added parameter, so everything after the ? follows ?added=parameter& .
I suppose that is not exactly hard, but alas, I lack the experience. And all I could find was something like "Take this specific defined query parameter you already know and set it as a path name" or vice versa, and I couldn't get that to work for my problem.
Is there a solution compatible with the syntax used elsewhere in the file? Or does that matter at all? Can I combine Redirect 301 … lines with RewriteCond … RewriteRule … commands? Does %{QUERY_STRING} help me somehow? If so, how can I figure out the correct syntax?
I would really appreciate if someone could point me in the right direction.
Many thanks in advance!

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

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.

redirect old wordpress ?page_id= to non-wordpress site

I used to have a WP site that I converted to a standard html site. Problem is I found doing a google search that instead of http://www.genealogyinc.com it was returning http://www.genealogyinc.com/?page_id=21, I dont know how many pages are like this but am trying to find a htaccess workaround, all the ones I found online give me 500 server errors.
Need a rewrite for any ?page_id= cause I dont know how many other numbers are out there.
Thanks
Off the top of my head, without testing, it would be something like this.
The first line looks for the page_id querystring parameter, and if it meets it, it should pass on to the second line. The rewrite rule I have below may need some tweaking, but I hope this helps you.
RewriteCond %{QUERY_STRING} page_id=(.*)$
RewriteRule $ /? [R=301,L]

.htaccess rewrite rule from mydomain.com/contact.htm to mydomain.com/contact

I guess this was a very common and simple .htaccess rewrite rule, however I wasn't able to google a solution for it.
So, the question is in the title already, how can I rewrite the address to change it from example.com/contact.htm to example.com/contact? The rule would of course not be only for just the contact.htm but for any page in the website. No need to worry about GET variables, since I won't be using any.
[ Also, do you think this is or might be considered a good practice or not really relevant? ]
Thanks.
Try this:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^contact.htm$ /contact
This should serve contact.html when requesting example.com/contact/
You could consider using MultiViews. You'll need to load the content negotiation module and turn MultiViews on, then Apache will automatically look for a file with an extension (there's a priority list in case you have both .html and .htm files with the same name for instance).

Why can't I make my URLs rewrite in my htaccess file?

I've been messing with this for the last hour... visiting about 20 different Google tuts and I can't seem make my site rewrite the url in any way. I've even just tried changing .php to .html and I'm not having any luck.
Here's one of my many fails:
RewriteEngine on
RewriteRule viewItem(.*)\.htm$ /viewItem.php?id=$1
For this above one, I'm just trying to change my viewItem.php page to viewItem.html
My ultimate goal is to change hxxp://mydomain.com/viewItem.php?id=900 (as one example of an id) to hxxp://mydomain.com/details/
I'm then going to append the title on the end. I can do the append in php but being a newb to .htaccess, I'm apparently missing something glaringly obvious.
Any ideas?
Thanks much.
Did you miss the 'l' of 'html'?
RewriteRule viewItem(.*)\.html?$ /viewItem.php?id=$1

Resources