mod_rewrite a sub directoy to child subdirectory - .htaccess

I have a following sub domain and folder structure
https://subdomain.maindomain.com/companyName1/area/dashboard
I want to mod_rewrite to this
https://subdomain.maindomain.com/companyName1/area/
So that everything in "dashboard" loads, in "area"
I would like the .htaccess file to live in the area folder, as there are multiple companyNames
I've tried placing the following .htaccess in companyName1/area
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-s
RewriteCond %{DOCUMENT_ROOT}/dashboard/%{REQUEST_URI} -s
RewriteRule ^(.*)$ /dashboard/$1 [L]
However when visiting https://subdomain.maindomain.com/companyName1/area/ it just loads the index.html of that folder, and not of the /dashboard folder

With your shown samples, attempts please try following htaccess rules. Please make sure to keep these rules at the top of your htaccess file.
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /companyName1/area/
##For external redirect to area link.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s.*/dashboard/?\s [NC]
RewriteRule ^(.*)/dashboard/?$ /$1/? [R=301,L]
##For internal rewrite to dashboard folder here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]

Related

Rewrite url in htaccess to correct

I have link like this: http://site.crm.int/project_discussions/test
I need redirect it to : http://site.crm.int/project_discussions/index.html?url=test
Text 'test' is changable. Can be for example 'december', 'meetting01' etc.
I tried it in my .htaccess:
RewriteRule ^\/project_discussions/(.*)$ \/project_discussions\/index.html?url=%1 [L]
But have 404 error.
Also tried this:
RewriteCond %{REQUEST_URI} ^/project_discussions/(.*) [NC, L]
RewriteRule ^(.*)$ /index\.html?url=$1 [L,NC]
And have 'Internal Server Error'
How can i fix it? thanks
With your shown samples, please try following .htaccess rules file. Make sure .htaccess file and folder project_discussions are residing in same root folder. Also your index.php OR index.html file should be inside your project_discussions folder.
Make sure to clear your browser cache before testing your URLs.
Also I have fixed your previous rule for handling internal rewrites.
RewriteEngine On
RewriteBase /project_discussions/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ $1/index.html?url=$2 [R=301,NE,QSA,L]

.htaccess redirect non-clean URL with two variables to clean URL

I am trying to redirect www.bostonmabl.com/400hitter/boxscore/?SeID=332&GmID=93091 to www.bostonmabl.com/400hitter/boxscore/332/93091/ in the event that a user enters an old non-clean URL. Below is my .htacess file which I tried placing in both the 'root' directory and the '400hitter' directory, yet neither is working. Any help is greatly appreciated.
RewriteEngine on
RewriteCond %{QUERY_STRING} ^SeID=([^&]+)&GmID=([^&]+) [NC]
RewriteRule ^/400hitter/boxscore/index\.php$ https://bostonmabl.com/400hitter/boxscore/%1/%2/? [R=301]
With your shown samples, please try following htaccess rules. Make sure your .htaccess rules file and index.php are residing in same folder.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect to new URL in browser.
RewriteCond %{THE_REQUEST} \s/(400hitter/boxscore)/\?SeID=([^&]*)&GmID=(\d+)\s [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]
##Internal rewrite in backend.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^400hitter/boxscore/(\d+)/(\d+)/?$ index.php?SeID=$1&GmID=$2 [QSA,NC,L]
NOTE: This solution assumes that you want to rewrite(in backend) to index.php(by seeing your attempted code) and pass SeID and GmID parameters/query string into it.

Htaccess how to add static text to url

I want to make example.com /productName to example.com /product/productName
I just want to add static text /product/ in mid to url how can i add this with .htaccess file to url structure? I already tried this code but it did not worked.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
#hiding parameter name and product.php
RewriteRule ^([^\.]+)$ /product.php?productName=$1 [NC,L]
With your shown attempts, please try following htaccess Rules. Please make sure to keep your htaccess along with folder product(not inside product folder).
Make sure to clear your browser cache before testing your URLs.
##Making rewrite engine on
RewriteEngine ON
##rules for external redirect
RewriteCond %{THE_REQUEST} \s/(product)\.php\?code=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Rules for internal rewrite here.
RewriteRule ^(product)/(.*)/?$ $1.php?code=$2 [QSA,L,NC]

How to rewrite url in htaccess to hide subfolder?

My website is located in a subfolder (gng2) of my public_html folder. The below code from the public_html/.htaccess file works fine in the sense that typing my domain loads the website properly from the subdirectory:
## redirect to subfolder containing the app.
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} !gng2/
RewriteRule (.*) /gng2/$1 [L]
My problem is that www.example.com/gng2/whateverpage also works. I want to rewrite/reroute these urls to www.example.com/whateverpage so that the "gng2" subfolder does not show up for example in Google Analytics results.
How should I modify the above code to achieve this?
Thanks,
W.
You can use this redirect rule in gng2/.htaccess to remove /gng2/ from all URLs:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+gng2/(\S*) [NC]
RewriteRule ^ /%1 [L,R=301,NE]
# other rules appear below this line
Please keep your htaccess file inside gng2 folder and try following rules in your htaccess rule file. Please make sure these rules are at top of your file(for applying http/HTTPS to URLs, in case you have more rules), also please do clear browser cache before testing your URLs.
RewriteEngine ON
## redirect to subfolder containing the app.
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
##Place your RewriteRule here.....

htaccess and rewriting urls

currently i have a /en/ folder that is empty except for a .htaccess with the following
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)$ ../index.php?language=en$1 [NC]
i use it to eliminate the need for
index.php?language=en
in all my URLs. I would like to modify the htaccess in a way that i no longer need the /en/ folder with nothing but the htaccess inside. ideally i would like an htaccess in my root folder that reads the url and if it is www.example.com/en/ to rewrite to www.example.com/index.php?language=en
This should work for you:
RewriteRule ^en/(.*)$ index.php?language=en$1 [NC]
Put the following code in .htaccess file in your root folder.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^en/(.*)$ index.php?language=en$1 [L]
Here $1 will append rest of the url as well. The condition will also help if you request your files using direct url.

Resources