Rewrite url name using with .htaccess - .htaccess

I have sample website name is example.com/customer/?loc=dashboard, this website is used PHP coding. I need to change last name ?loc=dashboard to dashboard. That means I want the result website name is mydomain.com/customer/dashboard
I have added a file called .htaccess in my root folder, and add something like this, but it cannot work:
ReWriteEngine On
RewriteRule ^/dashboard /?loc=dashboard [L]
I have refer this website https://mediatemple.net/community/products/dv/204643270/.htaccess-rewrite-rules to do. But cannot work. Hope someone can guide me on how to solve this problem. Thanks.
Updated - 1
Using #RavinderSingh13 method result:
Before ?loc=dashboard result:
Updated 12/11/2021 09:19:00
Rewrite rules
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com\.my [OR,NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com.my/$1 [R,L]
RewriteCond %{HTTP_HOST} ^example\.com\.my [OR,NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mydomain.com.my/$1 [R,L]
##External redirect to /customer/dashboard url rules here.
RewriteCond %{THE_REQUEST} \s/customer/?\?loc=(\S+)\s [NC]
RewriteRule ^ /customer/%1? [R=301,L]
##Internal rewrite rules to get it served by index.php file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^customer/(.*)/?$ index.php?parameter=$1 [QSA,NC,L]
Update - 2
everyoneknows.come.my/.htaccess
everyoneknows.come.my/index.php
everyoneknows.come.my/customer
everyoneknows.come.my/customer/index.php
everyoneknows.come.my/customer/account/dashboard.php
So that, in my php code, I use ?loc= replace account, then become everyoneknows.come.my/customer/?loc=dashboard
Update -3
I am using this method to get ?loc:
if (isset($_GET['loc'])) {
if (file_exists('account/' . $_GET['loc'] . '.php')) {
// if ($module_user_permission['view'] == 1) {
include_once 'account/' . $_GET['loc'] . '.php';
// } else {
// if ($_GET['loc'] != 'home') {
// include_once 'app/access_denied.php';
// } else {
// include_once 'account/dashboard.php';
// }
// }
} else {
include_once 'account/dashboard.php';
}
} else {
include_once 'account/dashboard' . (isset($_GET['system']) ? "_" . $_GET['system'] : "") . '.php';
}

With your shown attempts, samples; please try following htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
Make sure your htaccess rules file is present alongside with customer folder(not inside it). In 2nd rewrite rule I have added parameter to get it pass to index.php file you can set it as per your requirement.
RewriteEngine ON
##Applying www by external redirect rules here...
RewriteCond %{HTTP_HOST} ^example\.com\.my [NC]
RewriteCond HTTPS off
RewriteRule ^(.*)/?$ https://www.example.com.my/$1 [R,L]
##External redirect to /customer/dashboard url rules here.
RewriteCond %{THE_REQUEST} \s/customer/?\?loc=(\S+)\s [NC]
RewriteRule ^ /customer/%1? [R=301,L]
##Internal rewrite rules to get it served by dashboard.php file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^customer/(.*)/?$ customer/account/dashboard.php?parameter=$1 [QSA,NC,L]

Related

Redirect Old IDX home listing pages to Search Page

We are getting a lot of 404 errors on a Real Estate website after houses are sold and the listing goes offline. I am trying, via htaccess, to redirect the missing pages that Google Search Console shows as 404 to a home listing search page. I have tried the code below but it is redirecting all listing pages not just the ones that no longer exist. Not sure if it's my code or because the pages are dynamically created.
All the home listings are under www.example.com/homes-for-sale-details/[address]. If the listing no longer exists I want the page to redirect to www.example.com/homes-for-sale-details.
My htaccess code
# Redirect old home listing to a search page
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^homes-for-sale-details/(.*)$ https://www.example.com/homes-for-sale-details [L,NC,R=301]
</IfModule>
Where are I going wrong?
Many Thanks!
Edit (added more of htaccess code):
####################################
# START Redirect pages from old site
#
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^Properties https://www.example.com/home-listings [L,NC,NE,R=301]
RewriteRule ^Access https://www.example.com/search-homes [L,NC,NE,R=301]
RewriteRule ^Neighboorhoods https://www.example.com/neighborhoods [L,NC,NE,R=301]
RewriteRule ^Buyer-Resources https://www.example.com/buy-home-in-colorado-springs [L,NC,NE,R=301]
RewriteRule ^Relocation-Guide https://www.example.com/buy-home-in-colorado-springs/relocation-guide [L,NC,NE,R=301]
RewriteRule ^Buyer-Resources/Buyer-Finance/Finance-Information https://www.example.com/buy-home-in-colorado-springs/home-finance [L,NC,NE,R=301]
RewriteRule ^Seller-Resources https://www.example.com/sell-colorado-springs-home [L,NC,NE,R=301]
RewriteRule ^Area-Schools https://www.example.com/local-lifestyle/area-schools [L,NC,NE,R=301]
RewriteRule ^Colorado-Springs-Attractions https://www.example.com/local-lifestyle/colorado-springs-attractions [L,NC,NE,R=301]
RewriteRule ^Military-Bases https://www.example.com/local-lifestyle/military-bases [L,NC,NE,R=301]
RewriteRule ^About$ https://www.example.com/about-us [L,NC,NE,R=301]
RewriteRule ^contact$ https://www.example.com/contact-us [L,NC,NE,R=301]
RewriteRule ^Terms-Of-Service https://www.example.com/terms-of-service [L,NE,R=301]
RewriteRule ^Privacy-Policy https://www.example.com/privacy-policy [L,NE,R=301]
RewriteRule ^Site-Map https://www.example.com/sitemap [L,NC,NE,R=301]
RewriteRule ^neighborhoods/fountain$ https://www.example.com/neighborhoods/fountain-security-widefield [L,NC,NE,R=301]
RewriteRule ^neighborhoods/securitywidefield https://www.example.com/neighborhoods/fountain-security-widefield [L,NC,NE,R=301]
RewriteRule ^park-avenue-properties-blog https://www.example.com/blog [L,NC,NE,R=301]
RewriteRule ^Primary-Factors-the-Affect-the-Real-Estate-Market https://www.example.com/primary-factors-affect-real-estate-market [L,NC,NE,R=301]
</IfModule>
# END Redirect pages from old site
# Force HTTPS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
</IfModule>
# Remove "Blog" from blog post URLs and preserve blog paging
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !page
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^Blog/(.*)$ /$1 [L,NC,R=301]
</IfModule>
# Redirect old home listing to a search page
#<IfModule mod_rewrite.c>
# RewriteEngine On
# RewriteBase /
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteRule ^homes-for-sale-details/(.*)$ https://www.example.com/homes-for-sale-details [L,NC,R=301]
#</IfModule>
####################################
# Browser caching code removed :)
####################################
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I've tried to get this to work with the .htaccess file and could not. The issue with your script above is that you're not checking first for the 404 error, so all requests from that "folder" are being redirected. I've tried using the ErrorDocument directive in the .htaccess file with no luck, so the solution I worked out was to just use what wordpress already provides. In your themes subfolder (/wp-content/themes/THEMENAME) there should be a 404.php file. If it's not there you can create it. You can use some code at the top of this file to parse the request url and redirect the page. Something like this should work:
<?php
if (strpos($_SERVER['REQUEST_URI'], 'homes-for-sale-details') !== false)
header('Location: /homes-for-sale-details', true, 301);
else {
?>
// ... paste the current 404.php content here
<?php } ?>
Just remember to back up your current 404.php page in case you overwrite something you didn't mean to. If you want to try to get this working with the ErrorDocument stuff in htaccess be my guest, but for whatever reason, it wasn't working for me. Here's a post that shows how to do that.
Good luck!
After unsuccessfully trying to find a htaccess answer slim's answer got me thinging and I found this post WordPress Template Redirect (thank you so much for your help slim!!!)
Here is the modification I made to that code.
// Redirect missing home listing to a search page.
function __404_template_redirect()
{
if( is_404() )
{
$req = $_SERVER['REQUEST_URI'];
if ( is_file( $req )) {
return; // don't reduce perf by redirecting files
}
// check if "homes-for-sale-details" is in the URL
if ( strpos($req, 'homes-for-sale-details') == false ) {
return; // only redirect missing homes
}
// pull the parent directory and convert to site url
$parent_url = get_permalink( 1232 );
// redirect to parent directory
wp_redirect( $parent_url, 301 );
exit();
}
}
add_action( 'template_redirect', '__404_template_redirect' );

htaccess RewriteMode + Hide the original directory

So i have installed Laravel and also using htaccess rewrite so i can change the directory for the main domain. It's not the /public_html - its /public_html/examplecom/public
When i open the website: example.com (i have Laravel plugin which redirects me to example.com/en, and the website is fully visible.
The problem is when i try to open: example.com/examplecom/public ... it doesn't hide and return to example.com. It's redirecting to example.com/examplecom/public/en (the Laravel redirect).
So the htaccess code i'm using is this one:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/examplecom/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /examplecom/public/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ examplecom/public/index.php [L]
Can someone help? Thank you :)
Forget it... i made it with PHP...
For those who have the same problem:
$page = $_SERVER['REQUEST_URI'];
if (preg_match('|/examplecom/public/(.*)|i', $page)) {
preg_match('|/examplecom/public/(.*)|i', $page, $exit);
exit(header('Location: /'.$exit[1]));
}

.htaccess - Rewrite wildcard for a specific folder

I want to redirect anything that comes in on:
http://example.com/api/ /* e.g. http://example.com/api/anything */
to:
http://localhost:3002/api/ /* e.g. http://localhost:3002/api/anything */
Here's what I'm trying but fails:
RewriteEngine On
RewriteRule ^api/ http://localhost:3002/api/ [R=301,L]
I know I could do routes individually e.g.
Redirect 301 /api/thisaddress http://localhost:3002/api/thisaddress
But would rather have a wildcard... is there a way to do this?
UPDATE
As the web page is a single page app, I currently already have in my .htaccess file:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
RewriteRule ^ /index.html [L]
You can use:
RewriteEngine On
RewriteRule ^api/ http://localhost:3002%{REQUEST_URI} [NE,NC,R=301,L]
%{REQUEST_URI} represents original URI starting with /api/...
Alternatively, you can also use:
RewriteRule ^api/.*$ http://localhost:3002/$0 [NE,NC,R=301,L]

Redirect URLs with specific pattern to same URL in subsite

Update : By the help of answers, I am able to have this update. I want following expression in my .htaccess to be working . (Complete script of .htaccess is shared in question as well)
RewriteCond %{REQUEST_URI} ((mypage2(/\d{4}-\d{2}-\d{2})?/\d{1,2}) |about-us)
RewriteRule ^(.+)$ /subsite/#/$1 [R=301,NC]
Detail Of Question:
I have added angularjs subsite to an exsiting expressionengine php site.
subsite contains only three pages, which were very slow in existing website
I want htacces to redirect those three urls to new subsite urls e.g mydomain/page2/parameter1/parameter2 to mydomain/subsite/#/page2/parameter1/parameter2 and mydomain/page2/parameter1 to mydomain/subsite/#/page2/parameter1
(This is not compulsory) In subsite I want to clean up/manipulate Urls using history push state to show user
mydomain/page2/parameter1/parameter2 instaed of mydomain/subsite/#/page2/parameter1/parameter2
My .htaccess is like
RewriteEngine On
RewriteBase /
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/#/$1 [L,QSA]
Following is most wanted thing to me
RewriteCond %{REQUEST_URI} ((mypage2(/\d{4}-\d{2}-\d{2})?/\d{1,2}) |about-us)
RewriteRule ^(.+)$ /subsite/#/$1 [R=301,NC] ==> when url is like mypage2(/date optional)/pagenumber then redirect it to subsite/#/mypage2(/date optional)/pagenumber
Also this is optional, I can live if following is not achieved
In my routing.js I want to manipulate urls like
$rootScope.$on('$stateChangeSuccess', function (event, toState, toParams,
fromState, fromParams, options, Data) {
var cleanUrl = window.location.toString().replace(subsite+'/#/', '');
window.history.pushState(null, null, cleanUrl);
//alert("Yes this works, it shows me the required url in browser at this moment");
//Here I am trying to clean the url
//But after that it reloads, some unwanted redirection happens after it
});
Okay, Try with below rule,
RewriteEngine On
RewriteCond %{REQUEST_URI} ^!(aSpecialString2|aSpecialString1|/)
RewriteRule ^(.+)$ /#/$1 [R=301,NC]
Here you will not get the #/uri in url but you will get %23 html entity which will respond as same as # in url and your app will work.
And I am assuming you will get the way to work it out with angular.
You can use mod_rewrite and php in the server side to this task
Create an index.php
<?php
if ( !empty($_GET['id']) ) {
$protocol = 'http://';
if (!empty($_SERVER['HTTPS']) && ('on' == $_SERVER['HTTPS'])) {
$protocol = 'https://';
}
header('Location:' . $protocol . $_SERVER["HTTP_HOST"]. '/#/' . $_GET[id]);
exit();
}
?>
You html here
Add the following in your .htaccess
RewriteEngine On
RewriteBase /
RewriteRule "index.php" - [L]
RewriteRule "^(.*)$" "index.php?id=$1"
To bypass some urls aString1,aString2 you can use:
RewriteEngine On
RewriteBase /
RewriteRule "index.php" - [L]
RewriteCond %{REQUEST_URI} !^(aString1)
RewriteCond %{REQUEST_URI} !^(aString2)
RewriteRule "^(.*)$" "index.php?id=$1"
To bypass assets you can use:
RewriteEngine On
RewriteBase /
RewriteRule "index.php" - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule "^(.*)$" "index.php?id=$1"
Now, http://example.com/s1/s2 will redirect to http://example.com/#/s1/s2
You can use mod_rewrite and javascript snippet in the client side to this task
Add the following to .htaccess
RewriteEngine On
RewriteBase /
RewriteRule "^([^?#].*)$" "/?$1" [R=301,NC]
Add following to main html
<script>
window.onload = function() {
location.hash = '/' + location.search.substr(1)
}
</script>
Now, http://example.com/s1/s2 will redirect to http://example.com/?s1/s2#/s1/s2
As your question states clearly you only need your html client routes to always have # right after domain name, here is a simple way to achieve it
As you are asking this for angular, so its very easy in that case. Just paste following code in element of index.html
<script>
var currentSitePageUrl = window.location.toString();
if(currentSitePageUrl.indexOf('#/') == -1)
{
window.location = currentSitePageUrl.replace(yourDomainUrl+'#/')
}
}
</script>
Above will include # to each url request and rest of the url will stay as it is, but only when it is requesting some front-end page
If you have access to .htaccess on your server, you can modify it to work with the URLs in your code without inserting the #.
There are really 2 parts to this, one enabling HTML5Mode in your Angularjs app and 2 editing the .htaccess file on your server to deal with this successfully.
I don't PHP at all, but was able to get this working for several sites following this article: https://ngmilk.rocks/2015/03/09/angularjs-html5-mode-or-pretty-urls-on-apache-using-htaccess/#thecode
try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} /(mypage2(/.*)|about-us) # you used mypage2, so it will be redirected to subsite/#/mypage2
RewriteRule ^(.+)$ /subsite/#/$1 [R=301,NC]
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/#/$1 [L,QSA]
notice the comment of the first rule.
and you can try your rules in sites like this: http://htaccess.mwl.be/

301 Redirect Help with Dynamic URL's and Multiple Id's

I just did a redesign for www.wildchildclothes.com which has a new URL structure. The old site ran on Zen Cart and had a bad URL structure. Here are some examples of what I want to redirect from:
OLD: www.wildchildclothes.com/page.html?chapter=1&id=21
NEW: www.wildchildclothes.com
and...
OLD: www.wildchildclothes.com/index.php?main_page=faq_info&fcPath=0&faqs_id=13
NEW: www.wildchildclothes.com/customer-service.html
Here is what's in my .htaccess to achieve this:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^([^&]*&)*chapter=1(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*id=21(&|$)
RewriteRule ^page\.html http://www.wildchildclothes.com? [L,R=301]
RewriteCond %{QUERY_STRING} ^([^&]*&)*main_page=faq_info(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*fcPath=0(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*faqs_id=13(&|$)
RewriteRule ^index\.php$ http://www.wildchildclothes.com/customer-service.html? [L,R=301]
But this doesn't work at all - I only get sent to my 404 page instead of begin redirected. Can anyone shed some light? The full .htaccess is posted below.
Thanks much,
Jonah
None of the above solutions worked for me, but this does:
To redirect www.wildchildclothes.com/page.html?chapter=1&id=21 to www.wildchildclothes.com:
RewriteCond %{query_string} chapter=1&id=21
RewriteRule (.*) http://www.wildchildclothes.com/? [R=301,L]
To redirect www.wildchildclothes.com/index.php?main_page=document_product_info&products_id=280 to www.wildchildclothes.com:
RewriteCond %{query_string} main_page=document_product_info&products_id=280
RewriteRule (.*) http://www.wildchildclothes.com/? [R=301,L]
To redirect www.wildchildclothes.com/index.php?main_page=faq_info&fcPath=0&faqs_id=9 to www.wildchildclothes.com/customer-service.html:
RewriteCond %{query_string} main_page=faq_info&fcPath=0&faqs_id=9
RewriteRule (.*) http://www.wildchildclothes.com/customer-service.html? [R=301,L]
I'm sure there are better ways to do this but this works for me and .htaccess just gives me a headache so I'm not digging any further.
I hope this helps someone else out there!
Jonah
It would be easier and far better if you use PHP for the redirect. You could, for example, rewrite such requests to a PHP script that analyzes the URL arguments and redirects to the correct page. Maybe something like this:
// old-to-new.php
$rules = array(
array(
array('chapter' => '0', 'id' => '23'), // old URL parameters
'/customer-service.html' // new URL
),
// …
);
foreach ($rules as $rule) {
if (array_intersect_assoc($rule[0], $_GET) == $rule[0]) {
header('Location: http://example.com'.$rule[1], true, 301);
exit;
}
}
header($_SERVER['SERVER_PROTOCOL'].' 404 Not Found', true, 404);
exit;
And the corresponding RewriteRule:
RewriteRule ^page\.html$ old-to-new.php [L]
You want to allow more than one character in the query parameters. Change [^&]& to [^&]*&:
RewriteCond %{QUERY_STRING} ^([^&]*&)*chapter=1(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*id=21(&|$)
RewriteRule ^page\.html$ http://www.wildchildclothes.com? [L,R=301]
RewriteCond %{QUERY_STRING} ^([^&]*&)*main_page=faq_info(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*fcPath=0(&|$)
RewriteCond %{QUERY_STRING} ^([^&]*&)*faqs_id=12(&|$)
RewriteRule ^index\.php$ http://www.wildchildclothes.com? [L,R=301]

Resources