Redirect url to Seo friendly url htaccess - .htaccess

RewriteEngine On
#RewriteRule ^([a-zA-Z0-9]+)$ product-detail.php?asin=$1 [L,NC]
RewriteCond %{QUERY_STRING} ^product-detail.php?asin=[^&]+$ [NC]
RewriteRule ^([a-zA-Z0-9]+)$ $1 [R=301,L]
I want to redirect url to create seo friendly url like http://example.com/asinvaluecomesherewhichisuniqueinthedatabase
my sql query looks like this
if (isset($_GET['asin'])) {
$Product_asin = $_GET['asin'];
$product_query = "SELECT * FROM `tablename` WHERE `Asin` = '$Product_asin' ";
The link looks like
<a href="/fashe-colorlib/product-detail.php?asin=<?php echo $cat_Asin; ?>" </a>

<a href="/fashe-colorlib/product-detail.php?asin=<?php echo $cat_Asin; ?>" </a>
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ product-detail.php?asin=$1 [L,NC]
I had to remove the following part of the link.
product-detail.php?asin=
Now the working link looks like :
<a href="/fashe-colorlib/<?php echo $cat_Asin; ?>" </a>
I also got rid of this part :
RewriteCond %{QUERY_STRING} ^product-detail.php?asin=[^&]+$ [NC]
RewriteRule ^([a-zA-Z0-9]+)$ $1 [R=301,L]
It was suggested on another post , here on stack.
Finally, i am able to get
http://example.com/xxxxx
From
http://example.com/product-detail.php?asin=xxxxx

Related

rewrite subfolder doesn't work

Following rule doesn't work for subfolder rewriting.
RewriteRule ^cat/([0-9a-zA-Z]+) cat.php?id=$1
RewriteRule ^cat/([0-9a-zA-Z]+)/([0-9a-zA-Z]+) cat.php?id=$1&sid=$2
For example With this rule
<?php
$id='News';
$sid='Politics';
?>
..
In next page, while echoing $_GET['sid'], it doesn't work
Notice: Undefined index: sid in ...
But this rule
RewriteRule ^cat/([0-9a-zA-Z]+)/([0-9a-zA-Z]+) cat.php?id=$1&sid=$2
works, if only there is two querystring parameters
..
but it generate ERROR 500 if there is only one parameter
<a href="cat/<?php echo $id?>>..</a>
Try with below,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cat/([0-9a-zA-Z]+)$ cat.php?id=$1 [L]
RewriteRule ^cat/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)$ cat.php?id=$1&sid=$2 [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/

Rewrite Long URL using htaccess

If I have an URL like this :
http://www.example.com/book.php?translate=id&surah=1&verse=1&and=4&audio=on&hl=yes
and I want to reqrite to new URL like this :
http://www.example.com/quran/id/1/1/4/on/hl/
how to do this in htaccess?
If by the hl you meant yes this is what you want:
RewriteEngine On
RewriteRule ^quran/?([A-Za-z0-9]+)?/?([A-Za-z0-9]+)?/?([A-Za-z0-9]+)?/?([A-Za-z0-9]+)?/?([A-Za-z0-9]+)?/?([A-Za-z0-9]+)?/?$ book.php?translate=$1&surah=$2&verse=$3&and=$4&audio=$5&hl=$6 [NC,L]
TO use it in php you can modify put in the htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
And in the index.php
<?php
#In here you remove the path you don't want
$request = str_replace("/envato/pretty/php/", "",
$_SERVER['REQUEST_URI']);
#get the params by splitting
$params = split("/", $request);
?>
I don't know you use which version,but You can get that in documents
then you need set "URL_MODE"

.htaccess 301 redirect to the same page with no query string

I want to create a condition that if page has parameter in URL like ?print=1 - redirect this page to itself without any querystring.
Example:
I want this page:
http://sos-med.com/en/specific_page.html?print=1&ok=1
tp redirect (301) to the same URL with no Query string:
http://sos-med.com/en/specific_page.html
My code is:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^*print=*$ [NC]
RewriteRule ^(.*)$ %{REQUEST_URI}?
I have no test server, so can you tell me if my code is ok?
The code above is for every page on website. And before implementing that rule I would like to try the redirect for one specific page (see my example).
How to modify the code to work with "specific_page.html" only?
I want only .htaccess solution, not PHP code.
You're close, your %{QUERY_STRING} regex isn't right, and you're missing the 301 redirect flag:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^print=.*$ [NC]
RewriteRule ^(.*)$ %{REQUEST_URI}? [L,R=301]
Try that.
Thanks, and If I want to redirect single specific page: sos-med.com/en/aaa.html?print=1&ok=1 to sos-med.com/en/aaa.html ? –
Then you'd change what the rule matches against:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^print=.*$ [NC]
RewriteRule ^en/aaa.html$ %{REQUEST_URI}? [L,R=301]
Try this one instead :
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*&)?print= [NC]
RewriteRule ^(.*)$ /$1? [L,R=301]

htaccess url rewrite

i have a htaccess file which redirectes all the requests to index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ./index.php
My question is i would like to make a redirect to folder which doesn't exist and it should not affect the present htaccess redirects to index.php. since it is linked to entire website
for eg
domain.com/search=kannan&id=21
to
domain.com/kannan
I just need a way to allow only this request and everything else goes to index.php, any ideas...
You could use a condition to capture requests that match a specific pattern, like:
RewriteCond %{REQUEST_URI} ^domain.com/search(.+)$
Then do your rewrite. Or: http://www.google.com/search?q=htaccess+query+string+rewrite
i suggest you to pass id through form . Means use Form post method to pass id then you only need to have 'domain.com/kannan' in url and id will passed as post
Use similar type of code as below -
<form method="post" action="domain.com/kannan">
<input type="hidden" value="21" name="id" />
</form>
In this way form will be posted to domain.com/kannan with id = 21 and url will be domain.com/kannan
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /public/$1
#RewriteRule ^ index.php [L]
</IfModule>

Resources