htaccess / mod_rewrite for Search Engine Friendly URLs - apache

I have a bunch of files in the web root: -
/abc-674.php
/def-643.php
etc.etc.
I want to show these when the following URL's are requested, without changing the URL in the browser: -
/products/abc-674/this_can_be_anything.php
/products/abc-674/or_this.php
both redirect to /abc-674.php, and
/products/def-643/this_can_be_anything.php
/products/def-643/or_this.php
both redirect to /def-643.php.
So, basically, the bit between products/ and the next / is the target, while anything after that can effectively be ignored.
If it matters, I've already got a little code in my .htaccess to direct all traffic to my preffered domain (with www): -
# Direct all traffic to domain.com
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} !^domain$ [NC]
RewriteRule ^(.*) http://www.domain.com/$1 [R=301,L]
EDIT: To clarify - the 2nd last line in the above is because I access the site on my dev server via http://domain/ only, and so don't want that rewrite to apply for those requests. I do, however, want this new rewrite to apply to all requests.
Any help greatly appreciated!

I think, you can use the next rule in .htaccess:
RewriteRule ^products/([\d\w]+)/(.*)$ /$1.php

Related

301 Issues with 2 URLs on same webspace

I have 2 domains, the old domain is mjvandco.co.uk and he wants this redirecting to mjvlaw.co.uk. I have both pointing to the same webspace but when I test the URLs using https://httpstatus.io/ I get different results.
I have the following in my htaccess along with other stuff, but this is the redirect content:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mjvlaw\.co\.uk
RewriteRule (.*) https://www.mjvlaw.co.uk/$1 [R=301,L]
# Remove .html (excluding blog)
RewriteCond %{REQUEST_URI} !^/blog(.*)$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
The only URL that now is not right is this one: http://www.mjvlaw.co.uk/. I used this site
https://mjvlaw.co.uk - goes to https://www.mjvlaw.co.uk
http://mjvlaw.co.uk - goes to https://www.mjvlaw.co.uk
http://www.mjvlaw.co.uk - not work as does not go to https
https://www.mjvlaw.co.uk - fine
However, when I do the same for the old domain it all works as it should and every one below goes too https://www.mjvlaw.co.uk.
https://www.mjvandco.co.uk
https://mjvandco.co.uk
http://mjvandco.co.uk
http://www.mjvandco.co.uk
Am I doing something stupid here? Should I create another webspace and have one folder for the old domain and what for the current one and each having it's own htaccess file?
Thanks. I have done another ticket a month or so back but I am not sure how to change the questions, so I apologise for the similar ticket.
You rule only redirects non-www http URLs to SSL version of your site. To redirect both non-www and www http versions , replace your first rewrite block with the following
RewriteCond ℅{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.mjvlaw\.co\.uk
RewriteRule (.*) https://www.mjvlaw.co.uk/$1 [R=301,L]
Make sure to clear your browser cache before you test this.

having trouble writing htaccess redirects

I'm having trouble writing redirect rules in my website's htaccess file.
Basically, i want to write two rules:
1 - When i write the base URL, like http://www.example.com, i want it to automatically redirect the user to http://www.example.com/someDirectory.
2 - However, when i write http://www.example.com/Admin, i want it to redirect the user to http://www.example.com/Admin.
Here's what i've managed to do so far:
# This allows you to redirect index.html to a specific subfolder
Redirect http://www.example.pt http://www.example.pt/MainFolder
# This allows you to redirect index.html to a specific subfolder
Redirect http://www.example.pt/Admin http://www.example.pt/Admin
However this does not work... Any idea on how to do this?
Try it like this,
When there is no request for specific file or directory it will redirect you to your directory mention in rule and for the rest it will work without any rule.
Please check.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^$
RewriteRule ^ %{HTTP_HOST}/someDirectory [R,L]
After a long research i was able to find a solution to my problem. I'll leave it here, in case anyone's having the same problem:
#Rewrite everything to subfolder
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/MainFolder
RewriteCond %{REQUEST_URI} !^/Admin
Rewriterule ^(.*)$ MainFolder/$1 [L]

TLD specific 301 rewrite for relaunch on new server

the company I work for is relaunching their newly built website on a new server under the same domain as before.
Example:
www.website.com/company/info/news.html on server A is going to change to www.website.com/news on server B.
On server A there is a second website (mostly a clone, so the same links are used) under a different TLD (website.ca). So there exists also a www.website.ca/company/info/news which should not be redirected.
We are relaunching only website.com for now so I need TLD-specific redirects. Everything combined there are about 250 pages that need redirecting to the new equivalent.
Those redirects are mainly aimed at crawlers and bots so we do not lose our pagerank and also for visitors with bookmarks, so I will add the rewrite-rules to the .htaccess-file on both servers.
I'm having trouble finding examples for this very specific scenario, so this is what I got so far from various how-tos and tutorials:
RewriteCond %{HTTP_HOST} ^http://www.website.com/company/info/news.html$ [NC]
RewriteRule ^(.*)$ http://www.website.com/news [L,R=301]
From what I understand it should redirect http://www.website.com/company/info/news.html to http://www.website.com/news with the information that it is a 301 and case-insensitive matching for the URL I want to redirect.
If this is correct I will add 250 RewriteCond (1 for each "old" page) and 1 RewriteRule per "new" page (about 10 in total) in this style:
RewriteCond 1
RewriteCond 2
RewriteRule 1
Thanks for any help in advance!
When checking for domain name, it is correct to look into %{HTTP_HOST}, but since it contains only the domain name (without the REQUEST_SCHEME or REQUEST_URI) you should compare it to www.website.com
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
For your exact example, the .htaccess RewriteRule would be:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^company/info/news.html$ /news [L,R=301]
But such exact rules would require you to write new Cond/Rule pait for every single .html file. If your structure follows some pattern, maybe something like this would work:
RewriteRule ^company/info/([a-zA-Z0-9_-]+).html$ /$1 [NC,L,R=301]
That should rewrite
http://www.website.com/company/info/anything.html to
http://www.website.com/anything

Apache 2.2 - "Split" domain

I'm very new to Apache and already run into an problem which already takes a lot of time and I'm not even sure if it's possible.
I have two servers and one Domain called szop.in which is having an A record to my first server. On the first server I'm running an URL shortener called YOURLS, it's under szop.in/admin. I want the second server serve my homepage, therefor I want to redirect all requests like szop.in or http://subdomain.szop.in to the second server but not http://szop.in/admin.
Is this possible?
This doesn't seem to be the right solution and the mod_rewrite is causing me some headache:
RewriteEngine On
RewriteCond %{HTTP_HOST} szop.in [NC]
RewriteRule !^/admin$ hxxp://other-domain.in [R=301,L]
My idea was, since I need just one URL to work on the first server http://szop.in/admin, to redirect everything that is not starting with /admin to the other domain.
You almost got it:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^szop\.in$ [NC]
RewriteCond %{REQUEST_URI} !^/admin [NC]
RewriteRule ^ http://subdomain.szop.in%{REQUEST_URI} [R=301,L]
You cannot use the negation on the RewriteRule like that, you use it on a conditions.
This should do what you want, it verify if domain is szop.in and if folder is not /admin and redirect to subdomain.szop.in.

Mod Rewrite, Unexpected Results

We are trying to redirect everything from one domain to another with the following
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule .? http://www.example2.com%{REQUEST_URI} [R=301,L]
When we visit http://www.example.com/v2sc
We are being redirected to http://www.example2.comv2sc
We would like to be redirected to http://www.example2.com/v2sc considering www.example2.comv2sc is not a valid hostname
Any ideas on how we can accomplish this?
Thank you!
It seems like you're using a .htaccess file for this. In that context the leading slash is not present in %{REQUEST_URI} so it's up to you to put it back in.
RewriteEngine On
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^ http://www.example2.com/%{REQUEST_URI} [R=301]
Please also note that solutions like this should be used only if you cannot edit the main server configuration file. Doing so would allow you to use a cleaner combination of vhosts and Redirect directives that would run much more quickly.