want to add .html to certain urls (not all) using .htacess - .htaccess

I have some external sites pointing links (examples below) to my site that are not correct - they need to have .html added to the end. I have tried getting them changed on their side but cant - how can I do it so within .htacess I am manually adding the .html bit onto the urls I want to add it onto?
For example I have:
www.test.com/blue-boxes
and this needs to be www.test.com/blue-boxes.html
I am trying this:
RewriteRule ^(/blue-boxes)/?$ $1.html [L,R=301]
But its not working...any ideas?

This rule should work:
RewriteRule ^blue-boxes/?$ /blue-boxes.html [L,R=301]
if the url is www.test.com/blue-boxes or www.test.com/blue-boxes/ it will redirect to www.test.com/blue-boxes.html

Related

Issues redirecting old .asp site migrated to wordpress site

Summary:
I've been asked to migrate a .asp site over to Wordpress, but I'm having some issues with the .htaccess redirects.
What I'm trying to Achieve:
Pages:
domain.com/Some-Page-On-My-Site.asp
redirects too
domain.com/some-page-on-my-site/
Posts:
domain.com/articles.asp?title=The-Greatest-Post-Title
redirects too
domain.com/blog/the-greatest-post-title/
End Goal
Stay consistent with default Wordpress permalink structure using lowercase.
Convert only the .asp URL requests to lowercase not the rest of the site.
I'm thinking that because of the *.asp pages redirect that it may be sucking in the articles.asp and converting it to /articles/ . So I swapped their spot in the .htaccess file.
I'm thinking that RewriteCond and Skip Flag is probably required for this to work. But could be wrong.
I'm glad I was able to get the Pages redirect to work, although I think there is just a little more to make these work better.
What I've tried
This converts URL Requests for old .asp pages to new URL Structure:
RewriteRule ^(.*)\.asp$ /$1/? [L,R=301]
These attempts do not convert URL Requests for old .asp posts:
# Try 1
RewriteRule ^articles\.asp?title=(.+)$ /blog/$1/ [L,R=301]
# Try 2
RewriteRule ^articles\.asp?title=(.*)$ /blog/$1/ [L,R=301]
# Try 3 - Not that this would probably work but was an attempt
RewriteCond %{QUERY_STRING} (^|&)title\=(.+)($|&)
RewriteRule ^articles\.asp$ /blog/$1/? [L,R=301]
I confirmed that I can do 1-to-1 redirects, but not similar to the Pages structure like above where I can type anything before .asp and it just removes the .asp
RewriteCond %{QUERY_STRING} (^|&)title\=Duck\-Feet\-Wine($|&)
RewriteRule ^articles\.asp$ /blog/duck\-feet\-wine/? [L,R=301]
Does anyone have some insight on this? or am I crazy to think that I can do this?
I've read about 100+ pages and I'm stumped, including stumped on how RewriteCond works. I happy that I was able to get the pages redirect to work, now to convert a query string to a static like link to work with Wordpress's lowercase permalink structure.

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]

Rewrite URL and remove part with .htaccess

I am really stuck with my .htaccess file, and need some help :). I have a WordPress installation that I am using for testing. It is in a folder and I use .htaccess to get there. This is the rules I use so far:
######### Custom #########
RewriteEngine On
# ignore folders
RewriteCond %{REQUEST_URI} "/af1wp/"
RewriteRule (.*) $1 [L]
###############
# only for me #
###############
# HOME (Senne Tijdeman)
RewriteCond %{REMOTE_ADDR} ^###\.###\.###\.###$
RewriteCond %{HTTP_HOST} ^((www.)?([a-z0-9_\-]+).)?alleenf1.nl$
RewriteCond %{REQUEST_URI} !^/af1wp/$
RewriteRule ^(.*)$ /af1wp/$1 [L]
This works (with my real IP address of course), so no problem there. But now I want to rewrite exisiting URL's to a new format. The old URL is this:
http://alleenf1.nl/nieuws/QOgbb/raikkonen-alles-is-mogelijk-in-australi
The new URL should be this:
http://alleenf1.nl/raikkonen-alles-is-mogelijk-in-australi
The part I want to remove "nieuws/QOgbb/" is not always the same, so I have to use regex for that. But everything I tried did not work at all.
I thought this would be simple, but apparently not for me unfortunately. Now I have 2 questions.
What is the right RewriteRule to do this?
Where should I put it. In the .htaccess of the root folder, or the af1wp folder where the WordPress install is?
Tnx in advanced
To awnser the questions from poncha below:
Yes, the URL's always start with to folders. Just to clarify (was not clear) the part "nieuws" is always the same, but not the second part (call it an ID).
I prefer a redirect.
The file /raikkonen-alles-is-mogelijk-in-australi is a post in WordPress. That WordPress installation currently resides in the folder af1wp, but will be moved to the root folder when going live.
Try this:
RewriteEngine On
RewriteRule ^nieuws/([^/]+)/(.*) /af1wp/$1 [R=301,L,QSA]
This will only match URLs starting with "nieuws"
For now, the rewrite target is /af1wp/, change it to / when moving the wordpress.
When you move wordpress, you'll need to mix in this rule inside the wordpress rules, as it already has rewrite rules of its own - place this rule above its rules.
The flags used here:
R=301 - redirect with HTTP status 301 (Moved Permanently).
L - last rule (stop rules parsing after successful match of this rule)
QSA - query-string-append (append original query string to the rewritten request).

can not sitemap after url rewrite

I have written a rewrite rule in the .htaccess file for my website page:
RewriteRule ^nice_url/$ ?p_action=user_profile&post_author=45 [L]
The full link is: "http://www.example.co.il/nice_url/" it works perfect.
BUT, when I try to create a sitemap (e.g. with http://www.web-site-map.com/), I have an indication that the link "http://www.example.co.il/nice_url/" is broken.
Why?
The link is working fine. Why is it indicated as broken?
Thanks!
You can just add another rule to externally redirect long URL to nice URL like this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+\?p_action=user_profile&post_author=45\s [NC]
RewriteRule ^ nice_url? [R=301,L]
UPDATE: Alright I checked that sitemap generator is putting & instead of & in generated URLs.
You need to add this additional rule to handle this:
# convert & to &
RewriteCond %{QUERY_STRING} ^(.*)&(.*)$ [NC]
RewriteRule ^ %{REQUEST_URI}?%1&%2 [L,R,NE]
Seems that I found the problem:
I have a main .htaccess file that redirects to 3 different folders. Each folder is for a different website (all in the same host).
One of the websites is in WP. The rewrite happens well.
BUT, when I try to generate a sitemap - the wp returns a 404 for the sitemap generator and thats why I get an error.
solution: I'll probably try to rewrite from the php and not from the htaccess.

How do I rewrite the url?

Could someone tell me how to rewrite this URL. I have looked at a lot of questions on stackoverflow but they seem to be missing my answer.
RewriteEngine On
That is what I have... its a bit poor.
I need to rewrite url's if they do not point to a directory.
I need to do this...
any.domain.com/pages/some-page-slug/login
To be rewritten to the correct url of...
any.domain.com/pages/login.php?page=32
Does anyone have any ideas on how this can be achieved?
1) Rewriting product.php?id=12 to product-12.html
It is a simple redirection in which .php extension is hidden from the browser’s address bar and dynamic url (containing “?” character) is converted into a static URL.
RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1
2) Rewriting product.php?id=12 to product/ipod-nano/12.html
SEO expert always suggest to display the main keyword in the URL. In the following URL rewriting technique you can display the name of the product in URL.
RewriteEngine on
RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2
3) Redirecting non www URL to www URL
If you type yahoo.com in browser it will be redirected to www.yahoo.com. If you want to do same with your website then put the following code to .htaccess file. What is benefit of this kind of redirection?? Please check the post about SEO friendly redirect (301) redirect in php and .htaccess.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^optimaxwebsolutions\.com$
RewriteRule (.*) http://www.optimaxwebsolutions.com/$1 [R=301,L]
4) Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz
Have you checked zorpia.com.If you type http://zorpia.com/roshanbh233 in browser you can see my profile over there. If you want to do the same kind of redirection i.e http://yoursite.com/xyz to http://yoursite.com/user.php?username=xyz then you can add the following code to the .htaccess file.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
5) Redirecting the domain to a new subfolder of inside public_html.
Suppose the you’ve redeveloped your site and all the new development reside inside the “new” folder of inside root folder.Then the new development of the website can be accessed like “test.com/new”. Now moving these files to the root folder can be a hectic process so you can create the following code inside the .htaccess file and place it under the root folder of the website. In result, www.test.com point out to the files inside “new” folder.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
RewriteCond %{REQUEST_URI} !^/new/
RewriteRule (.*) /new/$1
TO do this you need to write a front controller.
See here, here, here, and here.
Alternatively in Apache you can rewrite this
any.domain.com/pages/32/login
or this:
any.domain.com/32/login
or even this:
any.domain.com/some-slug/32/login
to this:
any.domain.com/pages/login.php?page=32
One way or another to do this with only apache you need to supply the page id in some fashion. Keep in mind even with format any.domain.com/some-slug/32/login the content of the slug is irrelevant and won't necessarily link to the correct page. Which I imagine is undesirable and bad for SEO.
Another alternative is using RewriteMap. But this will be tricky and require reloading apache configurations whenever a page/slug is created/edit.
I understand that pages and login are static in this case and some-page-slug is changing. And you always want to redirect to static page /pages/login.php?page=32
So this is how to do it:
1) Rewrite
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32
or 2) Redirect Pernament
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32 [R=301,L]
or 3) Redirect Temporary
RewriteEngine On
RewriteRule ^pages/([a-zA-Z0-9_]+)/login(.*)$ /pages/login.php?page=32 [R=302,L]
Here is great article about htaccess trics
http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/

Resources