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

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]

Related

Eliminate duplicate directory name in url using .htaccess file

For some reason, I'm getting duplicate directory names in some urls within a subfolder on our website. This seems to affect only crawlers as the files within this directory work fine when navigated.
I'd like to simply remove the duplicate directory name and make mydomain.com/sub/sub redirect to mydomain.com/sub.
I've tried many versions but my .htaccess skills are lacking apparently. I currently have (not working of course):
RewriteRule ^mydomain.com/sub/sub/(.*) mydomain.com/sub/$1 [L,R=301]
RewriteRule ^mydomain.com/sub/sub/(.*) mydomain.com/sub/$1 [L,R=301]
The RewriteRule pattern matches against the URL-path only - you appear to have included (part of) the domain name. Also, mydomain.com in the substitution string is going to be seen as a relative subdirectory.
Assuming you have a limited number of subdirectories where this occurs then to reduce /sub/sub/<something> to just /sub/<something> you would do something like this:
RewriteEngine On
RewriteRule ^sub/sub/(.*) /sub/$1 [R=301,L]
If you have other directives in you .htaccess file, then this needs to go near the top.
First test with 302 (temporary) redirects to avoid potential caching issues. Clear your browser cache before testing.
But to echo #arkascha's comment... the reason why crawlers are finding these URLs in the first place would seem to be a fault in your URL structure/internal links - so this is what ultimately needs to be fixed.

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!

Redirect the URL using .htaccess file

I have a page showing the products with the hyperlink for it as
www.domainname.com/productname
now my client needs to add store and needs the URL to show as
www.domainname.com/store/productname
I have done it via code and now when I click on it for a detail page, its still redirecting to
www.domainname.com/productname
but need to be
www.domainname.com/store/productname
tried with this:
RewriteRule ^store/?$ domianname.com/?$ [NC,L]
in .htaccess file, not sure whether I'm on page
Can any one tell me how to do it via .htaccess file.
Your RewriteRule is backwards: you need the path you're matching first, then the path you're redirecting to. Try this:
RewriteRule !^/store/(.*) /store/$1 [NC,L]
Also, you don't actually need mod_rewrite to do this. You could try mod_redirect, which is simpler and easier to understand:
RedirectMatch !^/store/ /store/
(N.B. I haven't tried either of these, so I'm not 100% certain they do what you want.)

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.

Use htaccess to mask folder name

Here's a problem I'm always wanting to solve with htaccess. I haven't found a way yet, though it looks like it should be possible - perhaps someone can help.
Let's say I have a folder at the root of my site called /foo/. I want users to be able to access that folder at the path /bar/, but for various reasons I can't rename the folder.
So as not to create confusion I only want one path to ever be seen - that is to say, I don't want people to use the name /foo/ to access the folder; they should always use /bar/. If someone goes to example.com/foo/, their browser should redirect to example.com/bar/ - but the content returned should be the content of /foo/.
To make matters more complicated, pages in /foo/ have dependencies (images, stylesheets, links to other pages, etc) within /foo/ which are hardcoded and can't be changed. These must, of course, still work.
So, to summarise, this is what I want :
Requests for example.com/foo/ should redirect to example.com/bar/.
Requests for example.com/bar/ should return the contents of example.com/foo/.
Is this possible? It looks on the surface as if it would create an infinite redirect... but I'm pretty sure there are ways to prevent that in htaccess, aren't there?
I'd be very grateful for any help.
(PS - for a little extra background: The normal reason I want to do this is to rename the wordpress /wp-admin/ directory to something more professional and easy for customers to remember, such as /admin/. But the same system should work for masking any path in this way.)
I found a sort of workaround - by using a symlink and htaccess in combination.
First I created a symlink from /bar to /foo/.
Then I put this in htaccess :
Options +FollowSymLinks
RewriteRule ^foo/(.*)$ bar/$1 [R,L]
This has exactly the desired result - example.com/bar/ shows the content of the /foo/ directory, and example.com/foo/ redirects to example.com/bar/
But if anyone can come up with a pure htaccess solution I'd much prefer that!
Update :
Ok, I've finally found out how to do this. It turns out to be quite simple...
RewriteCond %{THE_REQUEST} ^GET\ /foo/
RewriteRule ^foo/(.*)$ bar/$1 [R,L]
RewriteRule ^bar/(.*)$ foo/$1
The only problem is that it doesn't take account of RewriteBase, so you have to include the full path in the first line (after ^GET\).
If I understand correctly what you want is something like this inside your .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^foo/$ bar/
RewriteRule ^bar/$ foo/
</IfModule>

Resources