Rewrite URL using .htaccess call without directory name - regex

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

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.

htaccess re-write for subfolders?

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]

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 a javascript file url with .htaccess file

I want to rewrite following url,
http://example.com/widgets/search.js?id=qerwtwttw45777
as follows,
http://example.com/widgets/search/qerwtwttw45777
and tried following rule in htaccess,
RewriteEngine on
RewriteBase /
RewriteRule search/(.*)/$ /widgets/search.js?id=$1
but it fails and showing
**500 Internal Server Error**
Anybody help to solve this problem.
Thanks
Place this code in /widgets/.htaccess:
RewriteEngine on
RewriteBase /widgets/
RewriteRule ^search/([^/]+)/?$ search.js?id=$1 [NC,L,QSA]
Make sure mod_rewrite is enabled.
Apart from the surplus slash / at the end of the pattern, the rule looks fine. So, it must be some other problem.
Maybe, mod_rewrite isn't active for your website. You could either look into the server's error log, if available or try just
RewriteEngine on
without anything else.
If it still has the 500 Internal server error, rewrite is most likely not available.

Htaccess redirect urls that match with Regular expression

I'm trying to redirect any url's that match the following criteria:
old-domain.com/marketplace/businesses/anything-here/ads/
old-domain.com/marketplace/businesses/anything-here/ads/anyhting-after-this-too
old-domain.com/marketplace/businesses/anything-here/ads/anyhting-after-this-too/anyhting-after-this-too/
etc...
to
new-domain.com/deal/
This is what my .htaccess file looks like
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^marketplace/businesses/([a-z0-9\-_]+)/ads/(.*) http://new-domain.com/deal/ [R=301,L]
</IfModule>
I've tried several variations os this including swapping the ([a-z0-9\-_]+) with ([A-Za-z0-9_-\s\.]+) and [a-zA-Z-_0-9]+) and other combos but I can't seem to get it to work.
Also notice where I said anything-here in the urls that means I want to be able to match pretty much anything that would be there.
Any help would be appreciated.
Thanks
The problem was that I had a physical directory at /marketplace/businesses/ with a .htaccess file that was preventing this from working.
I was able to add this to that .htaccess file and it worked like a charm.
Thanks to everyone who had a look and took a stab.
I think this might work. (Haven't tries it though)
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^marketplace/businesses/(.*) http://new-domain.com/deal/$1 [NC]
This will translate this
old-domain.com/marketplace/businesses/anything-here/ads/
old-domain.com/marketplace/businesses/anything-here/ads/anyhting-after-this-too
old-domain.com/marketplace/businesses/anything-here/ads/anyhting-after-this-too/anyhting-after-this-too/
to this
http://new-domain.com/deal/anything-here/ads/
http://new-domain.com/deal/anything-here/ads/anyhting-after-this-too
http://new-domain.com/deal/anything-here/ads/anyhting-after-this-too/anyhting-after-this-too/
Or like this i you want to keep the parts intact, try this
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^marketplace/businesses/([a-zA-Z0-9\-_]+)/ads/([a-zA-Z0-9\-_]+) http://new-domain.com/deal/$1/$2 [NC]
This will be translated to this
http://new-domain.com/deal/anything-here/anyhting-after-this-too
but I don't think the other two will match the regexp.