Htaccess Rewrite to remove directory - .htaccess

I need help creating a rewrite string to remove the ~12345678/ from the following: www.example.com/~12345678/file.
This doesn't appear on every link. What happened is during development links were created using the development url, there are a couple dozen links that appear this way. The rest of the links are fine. I already use a rewrite to remove index.php.

Try this (this will redirect any request for www.example.com/~12345678/dir/file.ext to www.example.com/dir/file.ext) :
RewriteRule ^\~[a-zA-Z0-9]+/(.*)?$ /$1 [L,QSA,R=301]

Related

Htaccess and redirect all content of folder, but exlude the main folder

I got the following urls:
domain.com/categoryA/articleA
domain.com/categoryA/articleB
I want to redirect:
domain.com/categoryA/articleA -> domain.com/categoryB/articleA
domain.com/categoryA/articleB -> domain.com/categoryB/articleB
but leave it as it is and do not redirect the main folder: domain.com/categoryA/
I tried to use the rule:
RewriteRule ^categoryA/(.*)$ /categoryB/$1 [R=301,NC,L]
but it also redirect domain.com/categoryA/ to domain.com/categoryB/
How to exclude from the above rewrite rule the redirection of the main folder (categoryA), but still redirect all that is in the folder (and then change also the root folder)?
I am looking for a solution that is SEO friendly (I got the same articles in two categories, but want still to have indexed domain.com/categoryA, but the rest only as domain.com/categoryB/xxx.
Best Greetings,
Mat
With your shown samples/attempts, please try following Rules in your .htaccess file. Please make sure to place this rule under your domain redirect rule(if its there), also make sure to clear your browser cache before testing your URLs.
RewriteRule ^categoryA/([\w-]+)/?$ /categoryB/$1 [R=301,NC,L]

Redirect one URL to another on same domain with htaccess

I am currently building a new website. The old website has different url's than the new one. Now i want to redirect, using htaccess.
Before going live, i want to test my rules locally.
The old website uses url's of this format:
www.domain.de/?content=whatevercontent
The new website uses url's of this format:
www.domain.de/index.php?content=differentcontentname
Which are rewritten (in the htaccess file, using several RewriteRules) to this format:
www.domain.de/nicecontentname
I tried Redirects like this:
Redirect http://domainfolder/?content=whatevercontent http://10.3.10.69/domainfolder/nicecontentname
This does not work.
After going live it should work like this:
Redirect http://www.domain.de/?content=whatevercontent http://www.domain.de/index.php?content=differentcontentname
..and then be rewritten to the nice url.
My Redirect-Rules just won't apply, i tried it in all combinations i could think of, with or without http, with or without the containing folder, using the already rewritten url or the actual one, etc..
Any ideas on this issue?
You can not use Redirect directive to manipulte querystrings. Here is a mod-rewrite example that works for QueryStrings :
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/\?content=this
RewriteRule ^$ /index.php?content=that [L,R]
This will temporary redirect /?content=this to /index.php?content=that
To make the redirect permanent (browser cacheable) change R to R=301.

How do I do a htaccess rewrite to another folder for a single file?

We moved a part of our site from one sub folder to another. I want to put permanent redirects (301) into htaccess for the files in this folder (some have changed their filename as well, so I can't just setup one rule for the whole folder). Here's what I'm trying
RewriteRule ^search/tutorial-search.html$ db/tutorial.php [R=301]
This doesn't work though, I get a 404 response when now entering the old URL. I find this curious as I had a rule in place for ages that does work, which looks like this:
RewriteRule ^search/tutorial-search.html$ search/tutorial-search.php
I really don't see the big difference. I also tried the following (among others) but it doesn't work either
RewriteRule ^search/tutorial-search.html$ db/tutorial.php
What exactly is causing this to fail? Just to make sure I put all of these at the exact same line of the htaccess file. Is it because I'm rewriting to another folder? Thanks :)
Try adding a leading slash to your rewrite targets, because when redirecting, apache could be mistaking a URL-path with a file-path.
RewriteRule ^search/tutorial-search.html$ /db/tutorial.php [R=301]

Simple and neat .htaccess redirect help required

This is a strange one...
A while back I managed to write a .htaccess redirect that worked so that the URL was read like: www.website.com/mt?page=index - and what the real URL of this page was www.website.com/PageParser.php?file=index.php
The problem has been that the FTP system of my webhost hides .htaccess files even though they are allowed and do operate - and so I have checked back on local copies I have of my .htaccess files and none of them have the code as to how this works - and I've forgotten how I did it!!
Essentially, I am using wildcards so that anything after mt?page= will actually be showing PageParser.php?file= but without having the PageParser.php showing within the URL (and this is the important bit, because the index.php on my site root is actually sent through PageParser.php first so that anything which shouldn't be there is wiped out before the end user sees it) - so how can .htaccess redirect/rewrite the URL so that any link to /mt?page= show the file located at /PageParser.php?file= without changing the URL the user sees?
RewriteEngine On
RewriteRule ^(.*)mt?page=(.*)$ $1PageParser.php?file=$2
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^page=([^&]+)
RewriteRule ^mt$ /PageParser.php?file=%1.php [NC,L]
This rule will rewrite (internal redirect) request for /mt?page=hello to /PageParser.php?file=hello.php without changing URL in browser.
Your source URL example (www.website.com/mt?page=index) has index while target URL (www.website.com/PageParser.php?file=index.php) has index.php. The above rule will add .php to the page name value, so if you request /mt?page=hello.php it will be rewritten to /PageParser.php?file=hello.php.php.
If there is a typo in your URL example and page value should be passed as is, then remove .php bit from rewrite rule.
The rule will work fine even if some other parameters are present (e.g. /mt?page=hello&name=Pinky) but those extra parameters will not be passed to rewritten URL. If needed -- add QSA flag to rewrite rule.
This rule is to be placed in .htaccess in website root folder. If placed elsewhere some small tweaking may be required.
P.S.
Better write no explanation (I knew it/I did it before .. but now I forgot how I did it) than having these "excuses". While it may be 100% true, it just does not sound that great.

.htaccess Rewrite Based on Existence of Path in URL

Here's the scenario, I have a website that used to be a static HTML site and WordPress blog using a subdomain (http://blog.domain.com).
I recently combined everything into a single WordPress installation. To maintain old links I had to rewrite requests like "http://blog.domain.com/index.php/2010/10/16/post-name" to "http://domain.com/index.php/2010/10/16/post-name". My problem is that when trying to visit just "http://blog.domain.com", I get redirected to "http://domain.com" when I want it to go to "http://domain.com/index.php/blog".
So, if a user requests "http://blog.domain.com" (by itself, with or without slash), I want it to go to "http://domain.com/index.php/blog". If they request an old URL of "http://blog.domain.com/some-link-to-a-post", I want it to redirect to "http://domain.com/some-link-to-a-post". In other words, if it's a URL to an actual post, I just want to strip the "blog" subdomain. If it's the old link to the main blog page, I want to remove the "blog" subdomain and append "/index.php/blog"
http://blog.domain.com/ -> http://domain.com/index.php/blog
http://blog.domain.com/index.php/2010/10/16/post-title -> http://domain.com/index.php/2010/10/16/post-title
Hopefully that's clear. I'm not an htaccess expert, so hopefully someone can help me out here. Thanks in advance!
Using the [L] command at the end of a rewrite will tell htaccess that this is the last rule it should match. If you put a rule to match your first condition at the top and the other rewrite rule you said you had already created after it, you should get your expected result.
Try this:
RewriteRule ^blog.domain.com(/?)$ domain.com/index.php/blog [L]
# Your other rewrite here #
I couldn't get that solution to work. However, I used the following:
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/index.php/blog/$1 [R=301,L]
That ends up in a URL like http://domain.com/index.php/blog/index.php/2010/06/04/post-title, but Wordpress is smart enough to fix it.

Resources