301 redirect Dynamic URL into Static URL - .htaccess

How to convert a 301 redirect dynamic url to a static url?
In this case
www.example.com/book.php?book=title
into
www.example.com/book/title
When Requesting the Dynamic URL it should Convert as a static URL.
The dynamic URL should be converted and the static URL should be displayed in the address bar instead.
I got this code :
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^user/([^-]+)/?$ /user.php?id=$1 [NC,L]
Can some one give me a 301 redirect of dynamic url tio static url.
Thanks in Advance.

The easiest way is using Apache's mod_rewrite in your .htaccess file. To get the 'static' (seo/fancy) url in the address bar, you'll need an external redirect. To get the server to do something sensible with it, you'll need an internal rewrite. To prevent an infinite loop, you'll need to use the %{THE_REQUEST} trick.
%External redirect
RewiteCond %{THE_REQUEST} ^(GET|HEAD)\ /book\.php\?book=(.*)\ HTTP
RewriteRule ^ /book/%1? [R,L]
%Internal rewrite
RewriteRule ^book/(.*)$ /book.php?book=$1 [L]
The above rules are untested (but they should work). If they work, change [R,L] to [R=301,L]. This will make the redirect permanent. This will cache the redirect in the browser and lessen the amounts of requests to your server and make search engines 'remember' the right url. You don't want to do this while testing though, because it might show a previous attempt (that is cached) instead of sending the request actually to the server.

Related

Changing my website from dynamic CMS to static HTML. Should I 301 redirect or use other methods?

I am changing my website from a dynamic CMS-system (Umbraco) to a static classic .HTML. It is the on the same domain, but the URL will change.
Example: The URL is changing from:
www.example.com/information
To:
www.example.com/info.html
My question is:
What is the best way to redirect while keeping the best SEO page rank.
I am thinking about 301 redirect through .htaccess, but I am not sure if I should redirect my new to .html urls to the old dynamic …/example - or the other way?
Or maybe there is a different better way?
I do have a fine 404.
Also I need the right redirect code for .htaccess - if that's the right way.
I hope you guys can help me out.
I haven't try anything out yet, because I don't wanna do 301 before the site go live.
You need to implement 301 redirects from the old URL to the new URL in order to preserve SEO and ensure that any "old" links that have been bookmarked or linked to from other websites still work.
Exactly how you implement the 301 redirect (either in your server-side script or in .htaccess) does not really matter. However, if you are moving to an entirely static site then .htaccess is likely the only option you have.
I am not sure if I should redirect my new to .html urls to the old dynamic …/example - or the other way?
You need to redirect from the "old" URLs to the "new" URLs that you are using/linking to on the new site. (It makes no sense to redirect the other way as that would just break everything!)
You can probably just use the simple mod_alias Redirect directive.
For example, to 301 redirect from /information to /info.html you could do the following:
Redirect 301 /information /info.html
Bear in mind that 301 redirects are cached persistently by the browser. To prevent caching issues it is advisable to test first with a 302 (temporary) redirect.
Have you considered keeping the same URLs? This would obviously negate the need for implementing redirects. You could employ URL-rewriting if the underlying file is called info.html. For example, using mod_rewrite:
RewriteEngine On
RewriteRule ^information$ info.html [L]
The above would internally rewrite a request for /information to info.html. The user only sees /information in the browser address bar, but info.html is served from your site.
Taking this further, it would be easier if the new "file" is simply the same as the old URL, just with a .html extension. For example, the URL is /information and the underlying file is information.html. You can then use a single rule to rewrite all your URLs. For example:
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^([^/.]+)$ $1.html [L]
The above assumes the old URLs do not contain additional slashes (ie. consist of a single path segment. In other words, all files are in the document root) and do not contain dots.
White
I finally got my page ready to go live, and i changed all my new URLS to the same name as the old URL, just with a .html extension - as u said. After that i used:
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^([^/.]+)$ $1.html [L]
And it works fine.
I do have a question about if a "RewriteRule ^(.*).html$ /$1 [L,R=301]" would be better? I mean both "/page.html" and "/page/" works, and this could mess with my former SEO ranking?
Also: what do u think of this:
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
And by the way. Do you use "page.html" or just "/page" in your href to prevent the .html showing?
//MM

substore redirect to subdomain

I have a URL like below
http://www.abcxyz.com/storename
and I have sub-domain with
http://subdomain.adbxyz.com
So I want to rewrite all URLs of http://www.abcxyz.com/storename with http://subdomain.adbxyz.com/
How can I do this using HTACCESS?
If you're using mod_alias and want to do just a permanent redirect then you can add the following line to your .htaccess:
Redirect 301 /www.abcxyz.com/storename http://subdomain.adbxyz.com/
You can't internally rewrite from one host (www.abcxyz.com) to another (subdomain.abcxyz.com) without using a reverse proxy. "Rewrite" means to change the URI internally on the server (or "behind the scenes") such that the browser or client doesn't know about it, the URL in the browser's address bar remains unchanged. To do this, using mod_rewrite in conjunction with mod_proxy:
RewriteEngine On
RewriteRule ^/?storename(.*)$ http://subdomain.adbxyz.com/$1 [L,P]
If what you mean is a "redirect", as in the request is made, and the response is to tell the browser or client to go to a different place, thus changing the browser's URL address bar, then that's simpler:
RewriteEngine On
RewriteRule ^/?storename(.*)$ http://subdomain.adbxyz.com/$1 [L,R]
This would go in the document root of the www.abcxyz.com vhost/server.

htaccess won't rewrite url

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^product/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/([a-zA-Z]+)/$ product.php?id=$1&srl=$2&item=$3&page=$4&title=$5#titleProduct
When I hit f5 button for refresh, the URL remains and it's not rewrited. I tried to access with the "new" rewrited link, but it's not working.
Wow, that means I'm need to update every single file for the "new" rewrite? All files contains different hyperlinks with the "old" link. I thought if I write a .htaccess file, all url will automatically rewrite.
You're thinking of a browser redirect, that changes the address bar, a rewrite takes the nicer looking URL and internally rewrites the URI to something the server can understand. See the top half of this answer for a better explanation of this process.
So you can do a browser redirect if the browser actually requests the product.php file, then redirect to the fake nice looking URL. The browser will then resend a new request, for the nice looking URL and the server gets that, internally rewrites it back to the php file (the rule that you have).
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /product\.php\?id=([^&]+)&srl=([^&]+)&item=([^&]+)&page=([^&]+)&title=([^&\ ]+)
RewriteRule ^product.php$ /product/%1/%2/%3/%4/%5/ [L,R=301]
This will take a /product.php\?id=123&srl=abc&item=qwerty&page=blah&title=something URI and redirect the browser with a 301 to the nicer looking URL. Then you're rule should internally rewrite it back.
Regardless, you really should change the links you serve to the nicer URLs, relying on mod_rewrite to do both ends of the work is really inefficient.

strip utm_source from url by htaccess then redirect to origin url

I have issues with URLs of website.
I have check the server and referrers, then see a lot of strange URLs with some addition var added to original. That may effect the SEO and server load to create those pages.
eg:
Original:
xaluan.com/modules.php?name=News&file=article&sid=123456
xaluan.com/modules.php?name=News&file=article&sid=123987
Someone access my web page by url
xaluan.com/modules.php?name=News&file=article&sid=123456&utm_source=twitterfeed&utm_medium=twitter
xaluan.com/modules.php?name=News&file=article&sid=123987&utm_source=twitterfeed&utm_medium=twitter
I need one .htaccess that can redirect all 301 URLs which have utm_source=twitterfeed&utm_medium=twitter to original one.
You could try this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^((.*?)&|)utm_
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1?%2 [R=301,NE,L]
It will drop everything from the Querystring after the first utm_.

301 redirect urls

I'm trying to redirect an old url to a new one using 301
I need an example of RewriteQueryString for the following 301? http://www .example.com/search/?depId=1&typeCatId=1 to the following http://www.example.com/mens/clothing
So when I type in the long URL in the browser, I am redirected to the new, shorter URL
Any ideas?
RewriteEngine On
RewriteRule ^search/\?depId=1&typeCatId=1$ /mens/clothing [R=301]
^ Try that.
You could use mod_rewrite either in your .htaccess file or the apache configuration. You might take a look at the RewriteMap feature if you are going to have a lot of different departments, etc. to map. Using the [R] flag after the RewriteRule will cause the browser to redirect instead of just being an internal redirect. Using [R=301] will make it a 301 redirect.

Resources