htaccess re-write for subfolders? - regex

Maybe someone can point me in the right direction with what I'm trying to do.
I'm trying to re-write so that all subfolders will re-direct to one folder while keeping the first subfolder info, along with the rest of the URL.
Example:
http://www.example.com/billy/profile/info.php
http://www.example.com/mike/profile/info.php
http://www.example.com/sarah/profile/info.php
http://www.example.com/mark/profile/info.php
where it would redirect to:
http://www.example.com/home/profile/info.php?user_name=$1
where I can get the user name and redirect to the correct page.

You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^(?!home/)([^/]+)/(profile/info\.php)$ home/$2?user_name=$1 [L,QSA,NC]

Related

.htaccess: Rewrite path internally without redirecting

I can access my web server as follows: https://www.example.com/my_old_folder/some_folder/
There's an .htaccess file in /my_old_folder/ with the following code:
RewriteEngine on
RewriteRule ^my_old_folder/(.*) my_new_folder/$1
I want to rewrite the folder my_old_folder internally to my_new_folder, without changing the URL in the browser. Just grab the files from /my_new_folder/ instead of /my_old_folder/. If there's another folder like /some_folder/ in this case, keep it. Only change the name /my_old_folder/ to /my_new_folder/.
Unfortunately, it's not rewriting the path, although I already tried many solutions from the internet, including the above one.
Who can help?
Inside /my_old_folder/.htaccess you can use this rule:
RewriteEngine on
RewriteRule .* /my_new_folder/$0 [L]
It is because all path matching is relative to my_old_folder/ inside /my_old_folder/.htaccess.

301 Redirect with Partial Filename Change

Here are some examples of the old URLs:
http://www.test.com/files/Old_Name.zip
http://www.test.com/files/Old_Name.php
http://www.test.com/files/Old_Name_Setup.php
http://www.test.com/files/Old_Name_100_Setup.php
Here are what the new URLs look like:
http://www.test.com/files/New_Name.zip
http://www.test.com/files/New_Name.php
http://www.test.com/files/New_Name_Setup.php
http://www.test.com/files/New_Name_100_Setup.php
I need to edit my .htaccess file to reflect these changes, so when people visit the old URLs, they are taken to the new URLs.
I've seen something like
Redirect 301 /files/Old_Name.php http://www.test.com/files/New_Name.php
Unfortunately, this way would require me to do this for every single file, and there are a lot. I'm guessing I'm going to have to use regex, but I haven't seen a solution online that only modifies part of the filename. I'm assuming this should be able to be done in 1 line as the structure change between old and new URLs is consistent.
You can use in /files/.htaccess:
RewriteEngine on
RewriteRule ^Old_Name(.*)$ New_Name$1 [NC,L,R=301]
Or in root .htaccess:
RewriteEngine on
RewriteRule ^files/Old_Name(.*)$ files/New_Name$1 [NC,L,R=301]

How to redirect all pages inside folder and remove .html prefix

I have found many 301 htaccess redirects examples but i need something specific and dont know how to build that exact redirect rule... the site i am build this for is a wordpress site.
Example:
http://www.example.com/article/some-article-01.html
http://www.example.com/article/data-analysis.html
http://www.example.com/article/another-page.html
To redirect to:
http://www.example.com/article/some-article-01/
http://www.example.com/article/data-analysis/
http://www.example.com/article/another-page/
looking for one htaccess rule that works in wordpress...
a pointer to relevant data would be accepted hapily. cant seem to find the right search query. An example code would be of course much more easier.
Insert this rule in your DOCUMENT_ROOT/.htaccess file as very first rule just below RewriteBase line:
RewriteEngine On
RewriteBase /
RewriteRule ^(article/[^.]+)\.html$ /$1/ [L,NC,R=302]
If it works for you then change 302 to 301 to make it permanent redirect.

Rewrite URL using .htaccess call without directory name

I really can't understand how to write htaccess lines. I was trying to Google for few hours and couldn't find anything relevant. Can anybody suggest me a simple .htaccess line that can let me navigate to http://www.mydomain.com/mydirectory/index5.html
by calling it as http://www.mydomain.com/required
I tried in the below manner but I didn't work for me.
RewriteEngine On
RewriteRule ^required mydirectory/index5.html [NC,L]
And, an other question if I place this .htaccess file in the mydirectory folder will that work or am I supposed to place this file only in the root folder?
Thanks.
Put this code in .htaccess in DOCUMENT_ROOT:
RewriteEngine On
RewriteBase /
RewriteRule ^required/?$ /mydirectory/index5.html [NC,L]
PS: You cannot keep this code in /mydirectory since your original URI doesn't contain /mydirectory
Reference: Apache mod_rewrite Introduction

Pattern matching for htaccess rewrite rules involving one folder to another

I need some quick help cleaning up the following rules for htaccess file, as the list is getting quite long.
This has occurred after a site move which makes author pages that previously came from /browse/author/NAME-HERE now sit at /by/NAME-HERE.
Surely there's a way to pattern match these or something?
Redirect /browse/author/some-person /by/some-person
Redirect /browse/author/another-person /by/another-person
Redirect /browse/author/some-other-person /by/some-other-person
Redirect /browse/author/yet-more /by/yet-more
Redirect /browse/author/ /
I'd like to keep the /browse/author/ redirect to the root though, if possible...
Many thanks for any help!
This is definitely possible with mod_rewrite.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^/?browse/author/(.*) /by/$1 [L,R=301,NC]