Simple ReWriteRule expanded with an anchor - .htaccess

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.

Related

htaccess redirect if the url does not contain a specific character

I'm moving the site to a subdomain and need certain tag strings to go to the subdomain and some to remain on the main site. Problem is both have a similar tag system.
I need this type of request
https://www.site.co.uk/tags/example-tag
to go here:
https://sub.site.co.uk/tags/example-tag
but this type of request
https://www.site.co.uk/tags/view?tags=14-some-varriable
to remain unchanged and parsed to content without redirecting.
What would be the most recommended and best solution?
I have written some code to work around other redirects but this one is causing me a headache.
Cheers
For your this mentioned example, below should work.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.+)/(.+)
RewriteRule ^ https://sub.site.co.uk/%1/%2 [R]

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.

How can I use mod_rewrite and preserve the url's fragment (anchor part)?

I'm taking a site that used to be static html (generated by an offline tool) and converting it to PHP. I'm sending all page urls to a single PHP file which parses the url. At first I just stuck FallbackResource MyPage.php in the .htaccess file, and it worked fine... except for urls with a fragment part. Many of the old urls were like /some-page.html#part2, and in the new scheme, part2 is a whole separate page, so I can't ignore that part of the url. With FallbackResource, all that MyPage.php would ever see is /some-page.html.
Now I've read some other questions on here about the topic, such as this one, and it seemed clear that mod_rewrite should in theory be able to do the job, but I can't get it to work. Here's my most current attempt:
Options +Indexes
DirectoryIndex MyPage.php
RewriteEngine on
#RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^:/.?#&]+\.html#(\w+) MyPage.php#$1 [NC,NE]
RewriteRule ^[^:/.?#&]+\.(html|php) MyPage.php [NC]
This works, again, for everything except anchors. It works no better than the old FallbackResource command did. I've tried juggling various parts of this in random ways, such as looking for %23 instead of #, omitting the NE flag, passing the value in a querystring instead of as a fragment, and whatnot.
I will note that I don't want to use redirection to translate the urls -- the PHP has to perform a lookup on it.
Well, I guess the answer is that it can't be done. The fragment part is never even sent to the server as part of the HTTP request. It stays inside the browser. Which means if anyone's saved an old link, it's just gonna go to the wrong page and that's all there is to it.
But I can write javascript into the page to redirect from the client. The PHP can iterate through the known list of old anchors on a given page and emit conditions to send them to the new page.

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

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]

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.

Resources