Rewrite Long URL using htaccess - .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"

Related

About RewriteRule .httaccess

well, i have a question, is there any possible chance to pass parameters from a tag and bring into?
i mean i have the a tag:
sent to $value categories
and in rewrite rule i don't know if i can send an random value
RewriteRule ^CATEGORIES index.php?page=categories [NC,L]
this is my actual CATEGORIES and i want to pass a value from a tag and to show like
RewriteRule ^CATEGORIES index.php?page=categories&anothervalue= [NC,L]
localhost/CATEGORIES/$value not like localhost/CATEGORIE?var = $value
and $value to be anothervalue value
Without messing up all my styles!
I work in localhost!
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)/([^\.]+)$ index.php?page=$1&value=$2 [NC,L]
Test :
URL : localhost/mycategory/test
index.php
echo $_GET["page"]
echo $_GET["value"]

.htaccess RewriteRule path to query string

I am trying to achieve that a url like
example.com/path1 to example.com/index.php?p1=path1
example.com/path1/path2 to example.com/index.php?p1=path1&p2=path2
In my .htaccess I have:
RewriteEngine On
RewriteRule ^(.*)/(.*)$ /index.php?c=$1&g=$2 [NC,L]
RewriteRule ^([a-zA-Z0-9-_]+)$ /index.php?g=$1 [NC,L]
It works well for the case where we have the example.com/path1/path2 but for the the case example.com/path1 only works if there is no dot in the url. For example, I want to example.com/mydomain.com working to example.com/index.php?g=domain.com but I am not able to make it working.
Could you help me with this please?
here's how I would handle this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?params=$1
then with whatever serverside language you're using I'll use PHP parse $_GET['param'].
$params = explode('/', $_GET['params']);

how to lowercase URL paramater in htaccess

I have to redirect URL with Upper Case parameter to URL with Lower Case parameter.
Since I don't have access to httpd.config file, help me with some pieces of lines to do it in .htaccess file.
For example:
www.domain.com/Whatsapp-For-Business-A-Beginners-Guide
to
www.domain.com/whatsapp-for-business-a-beginners-guide
Thanks in advance.
If you have the opportunity to use php:
You can do that. in the .htaccess (before actual RewriteRule):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule [A-Z] /linkChange.php?l=%{REQUEST_URI} [L]
and in linkChange.php :
<?php
$link = $_GET['l'];
$newlink = strtr($link," ABCDEFGHIJKLMNOPQRSTUVWXYZ","-abcdefghijklmnopqrstuvwxyz");
header("Location: http://www.exemple.com". $newlink,TRUE,301); exit;
?>

Rewrite PHP query string?

I've searched for other solutions to rewriting a URL using the .htaccess file at the root of my server, but the language is pretty confusing to me, and I can't seem to get it to work. How would I go about changing:
http://domain.com/share.php?media=059ogbuq70
to:
http://domain.com/059ogbuq70
I found code similar to this, and tried it, but it didn't seem to work:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^media=([0-9]+)$
RewriteRule ^$ share.php?media=$1 [QSA,L]
My PHP:
<?php
$media_id = $_GET['media'];
$url = file_get_contents("https://api.wistia.com/v1/medias/" . $media_id . ".json?api_password=my_key");
$json = json_decode($url, true);
$name = $json[name];
$origimg = $json['thumbnail'][url];
list($image, $size) = explode('?', $origimg);
$video = $json['assets'][5][url];
?>
I then echo the variables where I need them on my page.
Thank you!
To internally rewrite "pretty" URLs so that query strings are passed to PHP, I recommend the following .htaccess file:
RewriteEngine on
RewriteRule ^([^/]+)/?$ share.php?media=$1 [L]
The regex matches:
One or more characters that are not a backslash, optionally followed by a backslash.
Everything but the optional trailing slash are captured in $1 and rewritten as the "media" query string variable.
For example:
http://domain.com/059ogbuq70/
rewrites internally to
http://domain.com/share.php?media=059ogbuq70
Which means that:
$_GET['media'] = 059ogbuq70
You might find this mod_rewrite cheat sheet helpful for building your regex.
Also, you can test your rewrites here.
You need two rules for what you are trying to do. You can place this in the file called .htaccess
RewriteEngine on
#redirect all old URLs to the new rewritten URL
RewriteCond %{THE_REQUEST} ^GET\ /+share\.php\?media=([^&\ ]+)
RewriteRule ^ /%1? [R=302,L]
#rewrite folder path internally to share.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ share.php?media=$1 [QSA,L]

Search and remove the specific string from url using .htaccess rewrite

Using .htaccess apache rewrite I'm looking to remove subfolder/substring from the my url. For now it looks like:
www.domain.com/store/products.php`
and I want to change it to:
www.domain.com/products.php
How can i do it using .htaccess apache rewrite?
You can do that with the root .htaccess:
RewriteEngine on
RewriteBase /
RewriteRule ^store/(.*) /$1 [R=302,L]
Change [R=302] for [R=301] when test work well.
You need mod_rewrite. A rule will be something like this:
RewriteEngine On
RewriteBase /myapp/
RewriteRule ^/store/products\.php$ /products.php
Or, for many different files, e.g. products.php, places.php, blablabla.php:
RewriteEngine On
RewriteBase /myapp/
RewriteRule ^/store/([a-z]+)\.php$ /$1.php
That depends on what URLs you want to rewrite. You should read the docs.
All-matching .htaccess rule
Also, I'd suggest you to use the following catch all URLs pattern. It will redirect any path to index.php. This is widely used in a lot of CMS systems to give you ability to perform dynamic routing with PHP:
.htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [L,QS
index.php
<?php
$path = isset($_GET['path']) ? explode('/', $_GET['path']) : array();
// If you navigate to /foo/bar/dead/beef, yur $path var will be like:
// $path == array ( 0 => 'foo', 1 => 'bar', 2 => 'dead', 3 => 'beef' );
?>

Resources