I did a "site:" command in google to view the indexed urls and found a lot of urls with parameters all pointing to the same page.
Its a static site, but about 5 years ago it was a Wordpress site, which may be how those links were created and indexed by Google. The 3 main links I found are as follows. They all look like these 3 but with different id's.
http://example.com/index.php?option=com_banners&task=click&bid=41
http://example.com/?option=com_content&view=article&id=86&Itemid=201
http://example.com/phlebotomy-jobs?&pid=6774238282444518&q=Phlebotomy&pg=7
My question is how do I redirect these links, with all of the different parameter ids, to their corresponding pages. The first two go to the homepage and the 3rd one goes to a /phlebotomy-jobs page.
So, what I'm looking for is, for example, this url: http://example.com/phlebotomy-jobs?&pid=6774238282444518&q=Phlebotomy&pg=7 to redirect to http://example.com/phlebotomy-jobs
Basically removing everything from the ? on.
Well, not sure if it's the "best" way, but this, for example, works:
RewriteCond %{QUERY_STRING} option=
RewriteRule (.*) /$1? [R=301,L]
RewriteEngine On
RewriteRule ^index.php?$ /path/page.php [R=301,L,QSA]
RewriteRule ^$ /path/page.php [R=301,L,QSA]
RewriteRule ^phlebotomy-jobs?$ /path/page.php [R=301,L,QSA]
The QSA RewriteRule Flag appends the query string.
https://httpd.apache.org/docs/2.4/rewrite/flags.html
Edit:
Since you're not concerned with retaining the query strings:
RewriteEngine On
RewriteCond %{REQUEST_URI} (^/index.php$|^/$|^/phlebotomy-jobs$)
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) /$1? [R=301,L]
Note: This method prevents passing any query strings to either of those two pages.
Related
This is the full set of rewrite code in my htaccess file, but I'm only having problems with a conflict in the last 3 rules. The final rule writes the URL I want, but returns travel.php instead of travel2.php
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{http_host} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteRule ^(.+[^/])/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^journal/([^/]*)$ /journal2.php?url=$1 [L]
RewriteRule ^travel/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ travel.php?country=$1&url_string=$2
RewriteRule ^travel/([a-zA-Z0-9-]*)$ /travel2.php?cat=$1
RewriteRule ^travel/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ travel2.php?cat=$1&subcat=$2
This is what I'm after from those last 3:
/travel/country/title-url-string (currently works & displays correct content)
/travel/category (currently works & displays correct content)
/travel/category/subcat (currently displays URL as I want it, but returns travel.php content instead of travel2.php)
I originally had an ID number in the travel.php rule, and removing that has resulted in the conflict. I'm aware two of the rules have the same pattern, so how can I best go about getting the results I want? Thanks for any help. :)
From last 3 rewrite rules:
1 & 3 check the same pattern where first one always take effect.
Further, as your category and country values cannot be distinguished each other here, I would suggest to append a unique identifier to the url as below:
RewriteRule ^travel/country-([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ travel.php?country=$1&url_string=$2
RewriteRule ^travel/category-([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ travel2.php?cat=$1&subcat=$2
So your matching urls would be:
/travel/country-countryname/title-url-string
/travel/category-categoryname/subcat
My URLs look like http://example.com/?n=x. I want the URL to show as http://example.com/. I have used to approaches so far and none of them works.
First one is based on this question:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^n=1$
RewriteRule (.*) $1? [R=permanent]
After the answer below I modified the .htaccess file:
```RewriteEngine On
RewriteCond %{QUERY_STRING} ^n=(.*)$
RewriteRule (.*) $1? [R=permanent]```
but it still did not work. Here is the debugging info:
RewriteCond %{QUERY_STRING} ^n=(.*)$
This condition was met.
RewriteRule (.*) $1? [R=permanent]
The new url is http://example.com/blog/cat/??
Test are stopped, a redirect will be made with status code permanent
Second approach is:
RewriteEngine On
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ $1 [QSD]
None of them seem to work.
I have a few more questions:
Does rewriting only work if the URL is not typed manually?
Can I still access the values of query strings in PHP?
What happens when a user copies the rewritten URL?
The first approach is correct if you go to http://example.com/?n=1, if I am correct you should change ^n=1$ to ^n=(.*)$.
And the other questions:
It works for all kind. It doesn't matter if it was a robot or a human writing it, when you access a page, .htaccess will be read.
Yes you can. Use $_SERVER["QUERY_STRING"]. If you use the htaccess redirection before explained, you will lose them because you are redirecting.
What do you mean ?
Here is a quick SEO question. We decided to split our web site into two different sites. Google has already crawled 50K pages which we want to move to another domain name. My question is what would be the best way to deal with it as we want only certain URLs to be redirected not the whole website. Should I do mode rewire catch the get parameters and send them over to the new domain name? or should I do it with php headers?
olddomain.com becomes oldomain.com and newdomain.com
oldomain.com?name=jw&gsurname=black --> newdomain.com?name=jw&gsurname=black
oldomain.com with any other url structure should stay the same
You should be able to use the RewriteCond and RewriteRule directives together to match against query string values, like so:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^name=([^&]+)&gsurname=([^&]+)$
RewriteRule ^(.*)$ http://newdomain.com?name=%1&gsurname=%2 [R=301,L]
If you mean exactly that set of query parameters:
RewriteCond %{QUERY_STRING} ^name=([^&]+)&gsurname=(.*)$
RewriteRule ^/$ http://newdomain.dom [R=301, L]
The RewriteCond looks at the query string and checks that the first parameter is name and the second parameter is gsurname. The ([^&]+) collects all of the characters until it finds an ampersand (^& means not ampersand). The (.*)$ collects characters until the end of the query string ($).
If the RewriteCond is true, then the RewriteRule redirects to the new domain. The query string is automatically passed along as-is.
Hello i need to make my url clean and i just do not know were to start as it is mind boggling, i have read numerous things in regards to clean urls but i have no idea.
This what i am getting on woorank as i am doing my seo.
Warning! We've detected parameters in a significant number of URLs.
I am unsure if this is right i have taken my real domain out and put my site instead
#RewriteCond %{HTTP_REFERER} !^$
#RewriteCond %{HTTP_REFERER} !^//(\.)?My site/.*$[NC]
#RewriteRule .(png|gif|jpg)$ – [F]
RewriteCond %{HTTP_HOST} !^ My site.co.uk$ [NC]
RewriteRule (.*) My site.co.uk/$1 [L,R=301]
Thank you J C
A basic htaccess can look like this:
RewriteEngine on
RewriteBase /
RewriteCond %{http_host} ^mysite.co.uk [nc]
RewriteRule ^(.*)$ http://www.mysite.co.uk/$1 [r=301,nc]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?p1=$1&p2=$2&p3=$3 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?p1=$1&p2=$2 [L]
RewriteRule ^([^/\.]+)/?$ index.php?p1=$1 [L]
For every new parameters you just add ([^/.]+)/ and extra parameter in the end..
What happens in code is up to you, but you will need a standard way of working if you want to use something like this.. .
NOTE: you can't just implement this now, because your site needs to be fully build to the structure of your htaccess.. So if you would replace this now, alot of others things might get broken soon... .
It depends. As Naruto pointed out. We need to know the structure of your code. Give some examples of how the urls are now and what you want them to look like. The examples will explain a bit how you might have programmed the website.
e.g. different php file for each page /about.php, /register.php or a single entry /index.php with every page having the same parameter keys.
/index.php?page=page1&foo=bar&qux=norf
/index.php?page=page2&foo=bar
or perhaps each page has different parameter names
/index.php?page=page1&foo=bar
/index.php?page=page2&qux=norf
What you can always do is redirect to a single index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
index.php now needs to route everything approperiately. This basically does the same as what Naruto suggests but instead in php directly and this might be easier for you.
How this routing happens depends on your code and is up to you. But let us assume that you have a different file for each page with different parameters. You could do this without changing the rest of your code.
// FROM: /shop.php?category=software&subcategory=webdevelopment
// TO: /shop/software/webdevelopment
$path = explode('/',$_SERVER['REQUEST_URI']);
if($path[0] == 'shop') {
$_GET['category'] == $path[1];
$_GET['subcategory'] == $path[2];
include('shop.php');
}
This way only one file needs to be edited and the rest of your code can still work with the $_GET. This is the same as what you would do in your htaccess.
Ok, So I've looked at this topic for quite a while now and can't get anything to work, probably because I'm still having difficulty understanding it - So I'm going back to basics and asking this in the simplest of terms.
I have an empty .htaccess file
I have a current URL of http://www.website.co.uk/news.php?id=111111
I want this to become http://www.website.co.uk/news/111111
How Do I Do This?
Also please not that although this is the URL now, I'm planning on making some changes to the site so the URL's in the future may be:
http://www.website.co.uk/news.php?city=city&issue=1&title=the-title&id=111111
http://www.website.co.uk/news/city/issue/the-title/111111
How can I make it so that the future changes will work too? So far I have:
RewriteEngine On
RewriteRule ^news/(.+)$ news.php?id=$1 [L]
This still displays the full url and typing in news/111111 redirects to an error page. Please help!
Adding the following to your htaccess should work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^news.php$ /news/%1? [L,R]
RewriteRule ^news/(.*) news.php?id=$1 [QSA]
The above will change http://www.website.co.uk/news.php?id=111111 to http://www.website.co.uk/news/111111
and below will change
RewriteCond %{QUERY_STRING} ^city=(.*)&issue=(.*)&title=(.*)&id=(.*)$
RewriteRule ^news.php$ /news/%1/%2/%3/%4 [L,R]
RewriteRule ^news/(.*)/(.*)/(.*)/(.*) news.php?city=$1&issue=$2&title=$3&id=$4 [QSA]
http://www.website.co.uk/news.php?city=city&issue=1&title=the-title&id=111111 into http://www.website.co.uk/news/city/issue/the-title/111111
The values in %1, %2, $3, %4 gotten from the parameters after city=, issue=. title=. id=.
In city=London, London will be contained in %1 etc
The second RewriteRule will allow you to find the id used.