Static URL rewrite with htaccess hide full URL path and .html - .htaccess

My url is ukneurology.com and the url for one specific program is http://ukneurology.com/html/clinicInfo/stroke.html. I want to change the second URL to ukneurology.com/stroke along with every other page (they will all have a different path). I have seen a lot of dynamic pages, but none of my page URLs look like what I have seen in other examples. I just need an htaccess file where I can type in the real URL and then type in what I want it to look like in the browser and what people can use. Is there a template I can use? Or even better, a generator that will do it for me? I can't find anything!

You can use something like this: RewriteRule newPageName oldPageName [L] Pay attention: you have to escape . in the newPageName like this: \.. NewPageName and oldPageName don't include your domain.
In your example this would be right: RewriteRule stroke html/clinicInfo/stroke.html [L]
If the user tries to access http://ukneurology.com/stroke, he will see the page http://ukneurology.com/html/clinicInfo/stroke.html without a redirect (so the URL stays the same). The [L] avoids that any other RewriteRule will be met.
If you have a special scheme (for example http://ukneurology.com/abc => http://ukneurology.com/html/clinicInfo/abc.html), you can use a regex to avoid writing thousands of RewriteRules by hand: RewriteRule ([^/]+) html/clinicInfo/$1.html [L]

Related

Redirect dynamic url to new dynamic URL which is also prettified in htaccess

I have a website which has dynamic URLs and they're not currently prettified. I have re-worked the site and included pretty URLs, but the real dynamic folder structure/filename has also changed.
To better explain, the current dynamic URLs look like
http://example.com/liveguide/year.php?year=2017
The new dynamic url for the same page is
http://example.com/shows/show-list.php?year=2017
I use the following:
RewriteRule ^shows/(199[5-9]|200[0-9]|20[0-1][0-7])/?$ /shows/show-list.php?year=$1 [L]
To enable the use of pretty URLS like
http://example.com/shows/2017
So what I'm trying to do is if anyone followed a link of the original dynamic URL, they'll end up on the new clean URL. So far I've just got
RewriteRule ^liveguide/year.php /shows/show-list.php [R=301,L]
Which redirects to the correct page, but you're left with the ugly URL in the address bar. How could I do it so that the new, pretty URL is in the address bar?
Ie someone visits
http://example.com/liveguide/year.php?year=2017
They end up on, and see in their address bar
http://example.com/shows/2017
You just need to match the query string, which is in a separate variable, and used in a RewriteCond, which capture to %1, %2 etc. Like this:
RewriteCond %{QUERY_STRING} ^year=(\d{4})$
RewriteRule ^liveguide/year\.php$ /shows/%1? [R=301,L]
While we are here, do you really need to only match just those exact years? Would it matter if /shows/1234 also got rewritten? Probably not, you can just return a 404 from your PHP, so a simpler rule would be ok, like the above just saying any four numbers. It will also work for future years without changing it.
RewriteRule ^shows/(\d{4})/?$ /shows/show-list.php?year=$1 [L]
The goal is usually not to match exactly what you want, and only that, but rather to ensure nothing else (other parts of the site) will be matched that shouldn't be. Simpler rules are easier to maintain and review later. Your script must already be able to handle bad data anyway, so just let it handle the detailed checking without duplicating it unnecessarily.
Hope this helps.

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.

Simple ReWriteRule expanded with an anchor

Hi from Central Australia, Thankyou for your help
I have a simple re-write like this
RewriteEngine on
RewriteRule Trades-Services-(.*) Trades-Services.php?cat=$1 [L,NC]
what this does is turns the URL like this
http://website.com/Trades-Services-Plumber
into this
http://website.com/Trades-Services.php?cat=Plumber
what I hope for is to further (or compliment) that rewrite rule so it will turn this
http://website.com/Trades-Services-Plumber-123
into this
http://website.com/Trades-Services.php?cat=Plumber&ad=123#123
Where it throws on an anchor as well (#123)
the 123 could be any number and is always the same as the anchor
while still preserving that first functionality
it could also work with a URL like Civil-Celebrant instead of Plumber
e.g
http://website.com/Trades-Services-Civil-Celebrant-123
to
http://website.com/Trades-Services.php?cat=Civil-Celebrant&ad=123#123
Thankyou
To do the bulk of the rewrite, you'd need to change your existing rules and add the extra one to handle the second case:
RewriteEngine on
RewriteRule ^Trades-Services-(.+)-([0-9]+)$ /Trades-Services.php?cat=$1&ad=$2 [L,NC]
RewriteRule ^Trades-Services-(.+)$ /Trades-Services.php?cat=$1 [L,NC]
This should cover 3 of your 4 requirements, except for the first:
Where it throws on an anchor as well (#123)
The URL fragment, the #123 bit, is handled by the browser and when urls have a #name in it, the #name fragment is never sent to the server. So there's no way the server knows about the fragment, it's something the browser sees and the browser seeks to the anchor in the document. Something you could do is redirect to get the anchor. The first rule would then look like this:
RewriteRule ^Trades-Services-(.+)-([0-9]+)$ /Trades-Services.php?cat=$1&ad=$2#$2 [L,NC,R]
However, when someone enters http://website.com/Trades-Services-Plumber-123 in their browser's address bar, that rule will REDIRECT the browser to http://website.com/Trades-Services.php?cat=Plumber&ad=123#123, thus what's in their browser's address bar changes to the php URI. This isn't the same behavior you have in your original rule (which redirects internally on the server-side, preserving what's in the browser's address bar). The other thing you can do is when you generate links like http://website.com/Trades-Services-Plumber-123, ensure that there's a fragment added to it: http://website.com/Trades-Services-Plumber-123#123.

How to redirect an erroneous URL

I just noticed that sometimes (even when given a wrong url) load perfectly fine. How do they accomplish this? What I mean is, suppose you click on a link that seems good like www.foo.com but it contains in the end a space character which would appear on the address bar as www.foo.com%20 some sites manage to redirect this to their correct url while others just break. How can this be achieved? I'm guessing it's something to do with the .htaccess but I have no idea what to do or where to do it.
The URL I'd like to redirect looks like this actually: http://foo.com/%C2%A0
I get the following error message:
The requested URL /%C2%A0 was not found on this server.
How can I make this redirection?
So far I came up with:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^%?\ ]*\%
RewriteCond %{REQUEST_URI} !^/
RewriteRule ^(.*)$ http://www.foo.com/ [R=301,L]
but it's not working at all
URL Rewrite would be the IIS version that may exist in other forms if you want to look at re-writing the URL assuming you mean this kind of case.
Don't forget that browsers may make certain guesses about what someone enters so that if someone types in "foo.com " that the browser may trim white space by default rather than URL encode the text. If "http://foo.com" fails then it may try "http://www.foo.com" for another idea as these could be seen as simple interpretations to take on what someone types in. If both fail then it may just Google the text believing that the address bar should be treated like a search box.

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