My site passes a variable (a phone number) in the URL between pages. It grabs the phone number from the database and writes the URL link as: url.com/phone-number/id.
However, if the target page does not have a phone number it is replaced with a 0, so,
url.com/0/id.
In my .htaccess file to rewrite old query php parameters to clean urls I put a default url.com/0/id in the rewrite. Old pages did not have a phone number so all redirect will have the 0.
# 301 redirects ad
RewriteCond %{REQUEST_URI} ^/page\.php$
RewriteCond %{QUERY_STRING} ^id=(\d+)$ [NC]
RewriteRule ^page.php$ /page/0/%1? [R=301,NE,NC,L]
My problem is Google is indexing all new pages with the url.com/0/id and not url.com/phone-number/id, even though if you browse the site you will see url.com/phone-number/id for pages with phone numbers.
I am not sure if it is my URL rewrite or Google bot's behavior that is causing this.
Don't use R=301 for above rule as 301 means permanent redirect. Use 302 (temporary redirect):
RewriteCond %{QUERY_STRING} ^id=(\d+)$ [NC]
RewriteRule ^page\.php$ /page/0/%1? [R=302,NC,L]
Related
I have a hacked website where a hacker has injected e-commerce pages.
The pages are ranked on google but I want to redirect them to the homepage.
They have the following URL structure. The digits are randomly generated and they are almost 1000 on SERP. I have put random digits as an example:
https://example.com/?itemywdy11111111.html
https://example.com/?itemywdy51111.html
Below is my htaccess
RewriteEngine On
RewriteCond %{QUERY_STRING} ^itemywdy$
RewriteRule (.*) / [QSD,R=302,L]
The code above is not working at all.
How do I do a 301 redict using htaccess for these urls.
You should match only the starting part of query string leaving end anchor:
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)itemywdy [NC]
RewriteRule ^ / [QSD,R=301,L]
Also note use of (?:^|&) to let this string match anywhere in a query string which would allow this rule to work for your existing pages with other query strings as well.
This is the current rule in the htaccess
# Redirect
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ http://new.com/$1 [R=301,L]
</IfModule>
Works fine so far but it should have one exception: if it is the home page it should redirect to a specific url. So if its http://old.com/ it should redirect to http://new.com/shop in every other case it should behave like above so http://old.com/cat/xyz redirecting to http://new.com/cat/xyz and so on.
Any suggestions?
.* in regex matches everything including landing page URI i.e. / but .+ skips landing page.
You can use these rules:
RewriteEngine On
# handle landing page
RewriteCond %{HTTP_HOST} ^(?:www\.)?old\.com$ [NC]
RewriteRule ^$ http://new.com/shop [R=301,L]
# everything but landing page
RewriteCond %{HTTP_HOST} ^(?:www\.)?old\.com$ [NC]
RewriteRule . http://new.com%{REQUEST_URI} [R=301,L,NE]
301 a single page URL to another
To create a 301 redirect from one URL to another URL, add the
following line of code:
Redirect 301 /retiredpage.html http://www.example.com/newpage.html
You can add as many of these redirect lines as necessary to the .htaccess file.
301 a directory URL and all of its contents to another
If you have redesigned your site architecture and renamed a directory, you need to create a 301 for the entire directory. Here’s how:
RedirectMatch 301 ^/oldname/ http://www.example.com/newname/
301 a domain name URL to another
If you just bought an aged domain name whose traffic (and search page rank value) you want to use to augment that of your existing site’s domain, you can set up a 301 to transfer all traffic and ranking from the purchased domain name to your current site.
Use the following code as an example:
RedirectMatch 301 ^(.*)$ http://www.example.com
Be sure you set up this redirect code in the .htaccess file of the source site you want redirected, not the redirect target site!
301 domain name URL variants for canonicalization
Since search engines index URLs, having multiple URLs in the index that point to the same content page divides the available page rank credit for that page among those URLs. This is definitely a “not optimized for search” state of affairs! To learn more about the details of canonicalization, take a look at the Search Engine Land post Why Canonicalization Matters From A Linking Perspective. The bottom line is you want to consolidate the page rank to one (canonical) URL to optimize the search value of that content.
Once you understand canonicalization best practices, you’ll want to implement them on your site. That means you must account for all redirecting possible alternative URL variations to the canonical URL. Use the following code sample for your site’s home page:
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*(default|index)\.(html|php|htm)\ HTTP/ [NC]
RewriteRule ^(([^/]+/)*)(default|main|index)\.(html|php|htm)$ http://www.example.com/$1 [L,R=301]
The first two-line block of code redirects URLs that have omitted the “www.” prefix to the full “www.example.com” home page URL. That means the home page URL
**
http://example.com will not resolve on its own, but instead will redirect to http://www.example.com/.
**
The second code block redirects URLs specifying default page references to the URL that omits default page reference names. This code ensures that any home page URL that includes several versions of explicit page name references, such as default.htm or index.html, will be redirected to the canonical home page URL,
http://www.example.com/.
i need to do some simple 301 redirects as i am creating a site fresh and using the current domain.
How ever the website URl's all start with '?'.
For example:
Redirect 301 /?q=news http://websitedomain/news/
This results in the home page being loaded with the url still the same.
Is there any way to get around this?
You need to use mod_rewrite to match against the query string :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^q=([^&]+) [NC]
RewriteRule ^ http://example.com/%1? [NC,L,R]
I am trying to redirect any page on a site that contains a p=item parameter at any point in the url to a specific url on a different domain.
I've tried to piece it together from various instruction on how to do it via htaccess but to no avail.
Try:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)p=item(&|$)
RewriteRule ^ http://different-domain/url? [L,R]
I successfully changed my URLs from ugly ones with several parameters in the querystring to clean looking ones with the help of mod rewrite. However, there are many url's for my site. Rather than go back and edit the href attribute on each and every one of my anchor tags, I tried to write a redirect function in the .htaccess file that automatically redirects the old url to the new one.
In my .htaccess file, I have the following:
RewriteEngine On
Redirect teams.php?league=$1&team=$2&year=$3&tab=$4 teams/(.*)/(.*)/(.*)/(.*)
RewriteCond %{REQUEST_URI} !^(css|js|img)/
RewriteRule ^teams/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ teams.php?league=$1&team=$2&year=$3&tab=$4 [L]
No luck though... any thoughts?
Thanks
You need to do a check that the old URL with the php in it is actually being requested by matching against %{THE_REQUEST}, otherwise it'll redirect loop forever (e.g. user goes to team.php, serve redirects to teams, browser requests teams, server rewrites as teams.php, server sees "teams.php" and redirects to teams, browser requests teams, server rewrites as teams.php, etc. etc.)
RewriteEngine On
# redirect when the user actually requests for teams.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /teams\.php\?league=([^&]+)&team=([^&]+)&year=([^&]+)&tab=([^\ ]+)
RewriteRule ^teams\.php$ /teams/%1/%2/%3/%4? [R=301,L]
# internally rewrite any /teams/ URI
RewriteCond %{REQUEST_URI} !^(css|js|img)/
RewriteRule ^teams/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ teams.php?league=$1&team=$2&year=$3&tab=$4 [L]