.htaccess question - redirects after the domain name - apache

Apologies if this is answered elsewhere. I had a search for this on here, but I'm quite confused so I'm not 100% what to search for in the first place.
I have a Wordpress site which is at exampledomain.com. I also own exampledomain.co.uk, and I have put in the .htaccess file the follow lines:
RewriteCond %{http_host} ^exampledomain.co.uk [nc]
RewriteRule ^(.*)$ http://exampledomain.com/$1 [r=301,nc]
These work fine in terms of changing exampledomain.co.uk to exampledomain.com, but the moment I add in something after the exampledomain.co.uk (i.e. exampledomain.co.uk/page1) the .htaccess file doesn't change it so it tries to load.
Is there something I can add to the .htaccess file which will sort this, so that, for example, if I were to type exampledomain.co.uk/page1 it would redirect to exampledomain.com/page1 ?
Thanks,
Charlie
P.S. Apologise for the weirdly parsed example links, but as a new user it won't let me include more than two hyperlinks.

Why not simply do
RedirectPermanent / http://exampledomain.com
in the co.uk's config instead? mod_rewrite is very handy, but for a simple domain redirector, it's major overkill.
comment followup:
I'd go for something like this:
RewriteCond %{HTTP_HOST} exampledomain.co.uk$ [NC]
RewriteRule (.*) http://exampledomain.com/$1 [R=301,L]

Related

htaccess only one file in subfolder and keeping the url

I need to put files in a new folder like this:
domain.com/newsite/newpanel/events.php
There are other files in the server like:
domain.com/events.php,
domain.com/oldsite/events.php etc.
Requirement 1:
I cannot redirect everything (because old implementations exist) with generic rules,
so I only want to redirect specific urls.
domain.com/events should now skip the old files and go to domain.com/newsite/newpanel/events.php
Requirement 2:
I tried something like this
RewriteRule ^events /newsite/newpanel/events/$1 [P]
but the url on the url bar will change. Is it possible for it to display domain.com/events?
thank you all!
EDIT: Since OP has mentioned different URLs in comments section so adding solution as per that here.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/CProjects/events/?$ [NC]
RewriteRule ^(.*)$ CProjects/folderA/folderB/events [L]
Based on your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs after putting these Rules into your .htaccess file.
RewriteEngine ON
RewriteCond ^/events/?$ [NC]
RewriteRule ^(.*)$ newsite/newpanel/events.php [L]

RewriteRule url with a parameter to a new url with same parameter

With .htaccess I want to create a URL forwarding from
www.example.com/en/?instance_id=1083&lang
to
www.example.com/en/Veranstaltung/1083/
You can see, that the id is the same, it is possible?
The "en" in the url may change, it shouldn't be a problem.
I tried some different things, like this:
RewriteEngine on
RewriteCond %{query_STRING} instance_id=(.*)$$
RewriteRule ^(.*)$ /$1? [R=301,L]
But it didn't work.
I hope, you can help me. I have googled & checked other sites, I haven't been able to find a solution.
If you need more information, only ask me. Its my first question here.
Added the original links here:
http://dorfbladl.com/en/ <-- this is the page
If you press one entry of the calendar on the right side, it should be forwarded to
http://dorfbladl.com/en/Veranstaltung/1914/
This url is one example, the id changes with every entry.
Maybe it could be useful, to find the solution.
You can use the following :
RewriteEngine on
RewriteCond %{QUERY_STRING} instance_id=([^&]+)&lang
RewriteRule ^(.*)$ /en/veranstaltung/%1? [R=301,L]

.htaccess redirect according to PHP parameters

I'm trying to build some redirections with my .htaccess file to deal with some old forum url. I want the redirections to be made according to the PHP parameters (the topic ID).
For instance, something like
RewriteEngine On
RedirectPermanent /forum/viewtopic.php?t=123 /page1.html
RedirectPermanent /forum/viewtopic.php?t=345 /page7.html
RedirectPermanent /forum/viewtopic.php?t=89 /page3.html
The old and new URL are not related to each other (no PHP parameter has to be kept or something). I want to decide manually in my .htaccess file what to do for each topic ID.
But I can't manage to do that so easily. I tried many things, but nothing works.
Is this possible ? Any idea ?
Thanks a lot !
Edit : additional question : I want to add a global redirection of all the folder /forum to the root of the site ("/"). I guess I can place it after the others, so if no other rule is trigered, this one will be trigered.
I'm trying some things like
RewriteRule ^forum /? [L,R=301]
But everything I have tried so far redirects me to the "page1.html" (my first rule). Any idea why ? Thanks a lot !
You can't match against the query string using mod_alias's Redirect, RedirectMatch, etc. You need to use mod_rewrite and match against the %{QUERY_STRING} variable:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^t=123$
RewriteRule ^forum/viewtopic\.php$ /page1.html? [L,R=301]
RewriteCond %{QUERY_STRING} ^t=345$
RewriteRule ^forum/viewtopic\.php$ /page7.html? [L,R=301]
RewriteCond %{QUERY_STRING} ^t=89$
RewriteRule ^forum/viewtopic\.php$ /page3.html? [L,R=301]
NOte that RewriteEngine is a mod_rewrite directive, not mod_alias. So it has no affect at all on the RedirectPermanent directives.

Puzzled by Apache RewriteEngine

My webroot is example.com. I have two sub-directories below webroot, named sub1 and sub2. Inside sub1I've placed the following .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} test\.me$ [NC]
#RewriteRule ^(.*)$ http://example.com/sub2/foo.html [L]
RewriteRule ^(.*)$ /sub2/foo.html [L]
When someone tries to access http://example.com/sub1/test.me, I want them to be served http://example.com/sub2/foo.html.
The above .htaccess does not work. If I un-comment the RewriteRule with the full path, and comment out the RewriteRule with the path relative to the webroot, it almost works as intended (the right page is shown, but the URL shown is also changed from http://example.com/sub1/test.me to http://example.com/sub2/foo.html, which is not wanted).
I am not able to understand why the full path almost works, while the path relative to the siteroot don't.
And if anyone could explain why my approach don't work, and what the the "best practice" for doing this type of redirection is - I would be grateful.
I've solved it.
Unfortunately, I oversimplified the setting when I composed the question. In fact, in a "virgin" environment the original .htaccess works exactly as I intended it to work.
However, the setting I'm using this .htaccess is with Drupal 7, and this makes a difference, since Drupal 7 uses "clean URLs".
What I found is that rewriting to a "clean" url do not work. So the following rule in .htaccess do not work when redirecting to Drupal node #1:
RewriteRule ^(.*)$ /sub2/node/1
However, the following does:
RewriteRule ^(.*)$ /sub2/?q=node/1
Apologies for not putting all that was relevant for cracking it in the question.
I hope this answer helps someone that struggles with the same thing.

Beginner's apache mod_rewrite assistance

I am not really familiar with apache mod_rewrite.
I have url parameters such as {domain}/index.php?blog=5
I simply want to make it {domain}/home.php?client=5
Is it a task as simple as it sounds and can anyone help?
The following might work, give it a try
RewriteCond %{REQUEST_URI} ^/home.php [NC]
RewriteCond %{QUERY_STRING} client=([0-9]+) [NC]
RewriteRule (.*) http://%{REMOTE_HOST}/index.php?blog=%1 [L]
That seems pretty simple, to be honest — once you get your head into mod_rewrite, it's not that complex.
It sounds like you want to add
RewriteEngine on
RewriteRule ^/index.php?blog=(.+)$ /home.php?client=$1
to your configuration.
Some caveats:
If you are putting this in a .htaccess file, then remove the / from the RewriteRule line.
If you want to make this case-insensitive, add [NC] to the end of that same line.
If you want users to see the URL change (so sending a 302 Found redirection to the browser), then add [R] to the end of the RewriteRule line.
If you want both a 302 Found and for the URL to be case-sensitive, combine the two instructions as [NC,R] at the end of the RewriteRule line.
It's definitely worth reading the mod_rewrite docs, but the rule above should be all you need for this use-case.