ideas for a 301 redirect wildcards? - .htaccess

I'm having some trouble in finding an answer for a 301 redirect problem. The story goes like this:
I used to have a German language news site at www.punkto.ro. Every new article created with the site's CMS had the form: punkto.ro/articles/title-1234.html, where the 1234 is replaced by the article's number in the database.
For several reasons I had to redesign and opted for wordpress, which I placed in the root. I created a subdomain archive.punkto.ro and replaced the url variable "punkto.ro" in a config.php file with "archive.punkto.ro". Then I moved the site files to the subdirectory "archive". It works fine.
Of course, old article links in google now lead to a 404 response. To be sure, I also indicated the location of the archives on the 404, so people can go look there.
Now to the redirect: what I want to achieve is that when a user clicks a link in google punkto.ro/articles/title-1234.html, he/she should be redirected to archive.punkto.ro/articles/title-1234.html.
The strange stuff is that I can't find the folder "articles" in my file manager (cpanel OR ftp)... Does anybbody have any idea as to how the .htacces should look like?
Examples:
Old link: http://www.punkto.ro/articles/Staatschef_Basescu:_Rumaenien_einschliesslich_gesetzmaessig_fuer_Grexit_gewappnet-4169.html
New link: http://www.archive.punkto.ro/articles/Staatschef_Basescu:_Rumaenien_einschliesslich_gesetzmaessig_fuer_Grexit_gewappnet-4169.html

I don't think you have to create the .htacccess-file in the "articles"-folder. Just create it in the main folder.
I hope this will work for you:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?punkto.ro$
RewriteRule ^articles/(.*)$ http://www.archive.punkto.ro/articles/$1 [L,NC,R=301]
The RewriteCond makes sure that www.archive.punkto.ro will not be redirected again, the RewriteCond redirects every URL containing "articles/" to the archiv.

Related

How can I create friendly url redirection made form absolute url's in .htaccess?

I'm writing a program that creates custom friendly urls for websites in .htaccess file. The problem is that I want to create redirect from absolute friendly url to another absolute "real" url, because .htaccess file is placed in root directory and websites are placed in many different child directories.
If the pages were placed in the same folder, I could do something like this:
RewriteEngine On
RewriteRule site site.php
RewriteRule site-two site-two.php
but the pages are placed in different child folders, so the above code doesn't work.
I tried to do something like this but it doesn't work:
RewriteEngine On
RewriteRule http://www.randomdomain.com/sites-dir/site/ http://www.randomdomain.com/content/sites-dir/site.php
RewriteRule http://www.randomdomain.com/sites-dir/site-two/ http://www.randomdomain.com/content/sites-dir-two/site-two.php
I would not like the solution to be similar to 301 redirect because with it, .htaccess refers to another page, but does not save the friendly address in the url bar.
I also want linking pages using a friendly url to be possible.
Does anyone know the solution to this problem?
Thank you in advance for your answer.

htaccess rewrite url by matching only the first part of the url

I've been struggling to find a solution to this very simple problem. You would think that this is the classic "please help me I need to redirect this url to newurl" type of thing but, believe me, it isn't that easy.
I have a domain example.com, this example.com has some language variants:
example.com/mx
example.com/us
example.com/ca
example.com/cl
example.com/xx
and so on, example
.com is the index to choose between countries and it exists too but has no content.
We recently moved our example.com/mx to a whole new domain (with a different system) example.com.mx so we proceeded to permanently redirect every url there to the its new url in the example.com.mx
While we were at it, we discovered that our multilingual wordpress installation on example.com was duplicating all website's content in the root file, something which shouldn't have happened.
So, we have like
example.com/us/folder
example.com/cl/folder
example.com/ca/folder
etc
AND
example.com/folder which shouldn't exist
There are tons of directories that are duplicated there, we already know where to redirect each of these directories but we just can't find the right line of code to do it.
For example, we have set up this htaccess rule to deal with all the /mx/ folder, which works well:
RewriteRule ^mx/shop/ https://www.example.com.mx/? [L,R=301]
RewriteRule ^mx/home/ https://www.example.com.mx/? [L,R=301]
RewriteRule ^mx/page/ https://www.example.com.mx/? [L,R=301]
RewriteRule ^mx/tag/ https://www.example.com.mx/? [L,R=301]
But when trying to extend this same logic to the "/shop/" directory, it matches the same directory in EVERY country folder, something we obviously don't want.
This is what we tried, described previously:
RewriteRule ^/tag/ https://www.example.com.mx/newtagurl? [L,R=301]
This obviously also redirects example.com/ca/tag/, because the ^.
So my question is, how to properly redirect to these cases ONLY matching the beginning https://example.com/tag/ string instead of matching every /tag/ case in every country folder?
Thanks!

.htaccess redirects - updated site (301 appropriate?)

I've run into a problem updating my site, the google search results show up links to the old page which are 404's now, some of them even containing deprecated content.
My question is about the use of 301's. The old page had deeply nested pages like the example below:
www.site.com/category/subjects/subject_b.html
It shows up in google with a very specific description of 'subject_b', which is not optimal for my purposes.
The new layout I've been working on means that content that was once under '/category/subjects/subject_b' is now found in a single page (www.site.com/subjects.html), along with the hypothetical subject_a & subject_c.
Would I be wrong redirecting the old pages like this?
redirect http://www.site.com/category/subjects/subject_a.html http://www.site.com/subjects.html
redirect http://www.site.com/category/subjects/subject_b.html http://www.site.com/subjects.html
redirect http://www.site.com/category/subjects/subject_c.html http://www.site.com/subjects.html
Also, how would I deal with pages that have google descriptions with content which is not on the new equavilient page?
I'd be happy if anyone could shed some light on this for me, or point me in a right direction as to where I can read more about it!
Would I be wrong redirecting the old pages like this?
You'd remove the http://www.site.com part. This should suffice:
Redirect 301 /category/subjects/subject_a.html /subjects.html
Redirect 301 /category/subjects/subject_b.html /subjects.html
Redirect 301 /category/subjects/subject_c.html /subjects.html
Or even:
RedirectMatch 301 ^/category/subjects/subject(.*) /subjects.html
to cover for all 3.
If you have multiple domains all pointing to the same document root and you actually need to match the path for a specific domain, you can use mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?site.com$ [NC]
RewriteRule ^/?category/subjects/subject(.*) /subjects.html [L,R=301]

Website pages with slashes

Just building a site using php. I would like to know how to create pages without having to make ".php" links for every page. For example I want to build the site to be as follows: www.mysite.com/products instead of www.mysite.com/products.php. (Further example www.mysite.com/products/headphones )
An example of what I am looking for is as follows: http://www.starbucks.ca/store-locator
Any idea how starbucks is doing this? It isn't making a separate folder and index for every page is it?
Thanks in advance!
Jason
You can use URL rewriting to do this. You can create a .htaccess file, place it in the root directory of your website with the following content (as an example):
RewriteEngine On
RewriteRule ^tutorial/([0-9]+)/$ index.php?tutorial=$1
RewriteRule ^page/store-locator/$ index.php?page=store-locator
RewriteRule ^$ index.php
These are some basic rules that tell the web server to rewrite a URL without .php to a URL that does have .php.
In the above example, when you are visiting example.com/tutorial/3/, it will actually visit example.com/index.php?tutorial=3 and then your index.php will display the contents. It's similar for the store-locator page.

redirect old pages to new seo targets

i have a litte problem :(
on my site i created with mod_rewrite some rules...
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([0-9][0-9][0-9][0-9][0-9][0-9][^/]+)$ /index.php?lang=$1&i=$2&cat=$3&item=$4 [L]
with that rule i recieve the following link
/en/article/category/item/021205
with this code above is everything ok..
now my problem is that i changed my site for SEO.
the link looks now like this
/en/article/category/item/021205-seo-link-is-here
my problem are the pages in google and co..
is there a way to create rule that i can redirect:
/en/article/category/item/021205 >> to >> /en/article/category/item/021205-seo-link-is-here
my site is multilangual /en /es /fr if it is important for the rule
best regards bernte
If your want to do the redirect for each page on your own, then the order of your RewriteRules is important.
Better solution would be to ignore the seo-link in your index.php unless the articleId is unique

Resources