.htaccess parsing a phone number - .htaccess

I was wondering if anyone could help me with an htaccess rule.
I have a phone directory, and the folders are based on the phone number i.e. http://example.com/go/123-456-7890. But I'm finding a lot of users are refusing to use dashes and get an error message. I was wondering if there is a simple way to parse that with htaccess so they get automatically redirected to the right url format?
i.e. if they type http://example.com/go/1234567890 or http://example.com/go/(123)456-7890 that they get moved to http://example.com/go/123-456-7890

RewriteEngine On
RewriteRule ([0-9]{3})([0-9]{3})([0-9]{4}) /go/$1-$2-$3 [R]
This only does solid numbers - I am not great # mod_rewrite but this is a start:)

These 2 rules in your root .htaccess should take care of that:
RewriteEngine On
RewriteRule ^(go)/(\d{3})(\d{3})(\d{4})/?$ /$1/$2-$3-$4 [L,NC,R=302]
RewriteRule ^(go)/\((\d{3})\)(\d{3}-\d{4})/?$ /$1/$2-$3 [L,NC,R=302]

Related

.htaccess url rewrite multiple directories to GET parameters

Ok so I have a site that I want to write rewrite rules for. I don't have much experience with rewrites.
My .htaccess is in /foo/, not the root. Here's what I want
Case 1: example.com/foo/bar/ --> example.com/foo/includes/page.php?school=bar&id=
Case 2: example.com/foo/bar/1 --> example.com/foo/includes/page.php?school=bar&id=1
Here is what I have
RewriteEngine on
RewriteRule ^(bar|baz)/(.*)$ ./includes/pages.php?school=$1&id=$2 [NC,L]
There are only two possible values for school bar and baz. The id can have any number of values.
The above code works in Case 2 but doesn't work in case 1. It seems to externally redirect in case 1 to different urls depending on if there is a trailing slash or not. No idea why. Any help would be greatly appreciated.
You can put this code in your htaccess (in foo folder)
RewriteEngine On
RewriteBase /foo/
RewriteRule ^(bar|baz)/([0-9]*)$ includes/page.php?school=$1&id=$2 [NC,L]

htaccess mask a range of URL's to more readable versions but show content from original URL

Here is a sample of filter URL's on an ecommerce store.
http://www.domain.com/showering/showers/filter/alliance
http://www.domain.com/showering/showers/filter/aquaflow
http://www.domain.com/showering/showers/filter/grohe
http://www.domain.com/showering/showers/filter/mira
I'm wondering if there is a way I can mask these URL's so they appear like:-
http://www.domain.com/alliance-showers
http://www.domain.com/aquaflow-showers
http://www.domain.com/grohe-showers
http://www.domain.com/mira-showers
But still display the page content from the /showering/showers/* URL's?
I then wish to be able to set the canonical URL's based on these masked URL's.
I've played around with countless variations with little success but here is something I've got so far:-
RewriteEngine on
RewriteCond %{HTTP_HOST} !^/showering/showers/filter/(alliance)
RewriteRule (.*) /alliance-showers/
When this is applied to website, all the images on the Magento store don't load incidentally along with the fact that the URL doesn't change at all.
Answer to #anubhava's comment...
.htaccess file is in root or Magento installation. It is the very first rule in file like so:-
############################################
## enable rewrites
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^-]+)-([^/]+)/?$ /showering/$2/filter/$1 [L,R]
Currently testing with this URL:-
http://www.showermania.co.uk/showering/showers/filter/alliance
Wanting to show as:-
http://www.showermania.co.uk/alliance-showers
Current answer has no affect/change on this URL at all. Thanks.
You can use a rule like this:
RewriteEngine on
RewriteRule ^showering/([^/]+)/filter/([^/]+)/?$ /$2-$1 [L,NC]

htaccess rewrite query string nothing works

THE PROBLEM
After looking at 50+ StackOverflow posts and trying many permutations of my htaccess file, it does nothing still.
WHAT I HAVE TRIED
Using this website to generate my htaccess file: http://www.generateit.net/mod-rewrite/
Setting AllowOverride All in my httpd.conf file and restarting Apache.
MY CURRENT HTACCESS FILE
Lives in the root directory.
RewriteEngine On
RewriteBase /
RewriteRule ^find-a-local-doctor/([^/]*)/([^/]*)$ /find-a-local-doctor/?state=$1&city=$2 [L]
WHAT I WANT TO ACCOMPLISH
Change this URL:
http://www.md1network.com/find-a-local-doctor/?state=FL&city=Tampa
To this:
http://www.md1network.com/find-a-local-doctor/FL/Tampa
ADDITIONALLY
Since the actual file doing the work is: http://www.md1network.com/find-a-local-doctor/index.php, I need to be able to parse the query string with PHP. Hopefully, I will still be able to do this to get the state and city.
Please help.
Thanks.
Your existing rule looks alright but you will need an additional external redirection rule for reverse. Put this rule before your existing rule (just below RewriteBase /).
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(find-a-local-doctor)/(?:index\.php)?\?state=([^&]+)&city=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]

.htaccess, get the value after a period and then redirect

I am trying to redirect a URL using a .htaccess file. The URL structure is like:
http://mydomain.com/folder/.anything_goes_here
Note the dot in the above Url. I want to remove it somehow using .htaccess.
I have tried using RewriteRule but it's not working.
Here is the code I used:
RewriteEngine on
RewriteBase /
RewriteRule ^/folder/.(.*+)/?$ /folder/$1 [L]
Any help would be highly appreciated. Thank you.
The period has a special meaning in regular expressions (it means "any character"). In order to explicitly specify a period, you need to escape it.
RewriteRule ^/folder/\.(.*+)/?$ /folder/$1 [L]

How to strip trailing URL characters in htaccess

I'm hoping someone can help with this one. I run a forum written in Perl, and the forum does something to URLs that is causing search engines to create duplicates.
I'm thinking that the best way of handling this is to sort it at the htaccess level.
As an example, the following 4 URLs all go to the same page, but search engines are seeing one entry with three duplicates:
http://www.domain.com/forum/YaBB.pl?num=1234567890
http://www.domain.com/forum/YaBB.pl?num=1234567890/2
http://www.domain.com/forum/YaBB.pl?num=1234567890/19
http://www.domain.com/forum/YaBB.pl?num=1234567890/22
I'm looking get htaccess to redirect anything that has a forward-slash somewhere in the last three characters, to a URL that has the slash and trailing numbers removed. Using the above example:
Redirect 301 /forum/YaBB.pl?num=1234567890/2 to /forum/YaBB.pl?num=1234567890
Alternatively, to re-write URLs from that subdomain to strip "/n" and "/nn"
Anyone have any ideas?
Try this:
RewriteEngine On
RewriteBase /
RewriteRule ^cgi-bin/forum/YaBB\.pl\?num=([0-9]+)/[0-9]+$ cgi-bin/forum/YaBB.pl?num=$1 [R=302,L]
This should work, but let me know if it doesn't :) Also if it works, change the 'R=302' to 'R=301'
Try this rule:
Options +FollowSymLinks +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^num=(\d+)/
RewriteRule ^(cgi-bin/forum/YaBB\.pl)$ /$1?num=%1 [R=301,L]
Tested on local Apache installation -- works fine for me.

Resources