Replacing Strings with .htaccess in URL - apache

I need to edit my .htaccess file so that this string:
https://example.com/haathumb.php?var=400x300/src/wp-content/uploads/2017/08/brain-training-game-show-app-improves-memory.jpg
Is replaced with this replaced with this string:
https://example.com/thumb/400x300/src/wp-content/uploads/2017/08/brain-training-game-show-app-improves-memory.jpg
To sum up I need to replace haathumb.php?var= with thumb in a string using .htaccess. How do I do that?

A redirect from haathumb.php?var=... to thumb/... then?
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)var=([^&]+)
RewriteRule ^haathumb\.php$ /thumb/%1? [L,R=permanent]

I ended up using the .htaccess code below to make haathumb work correctly:
# BEGIN haathumb rewrite
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^thumb/(.*) /haathumb.php?var=$1 [L]
</IfModule>
# END haathumb rewrite

Related

Why is mod_rewrite adding var/www/html to the resulting url

I am trying to make my service backward compatible, since I have moved the service to a new path internaly, I still want the users to access it with the old url as not all of them have knowledge of this change.
I can accomplish what I want if I add [R] flag at the end of my rewrite rule but it redirects the url on the client side, which I don't want.
My rewrite code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/old-path(.*)$ /new-path/$ [L]
</IfModule>
Although this rule results in the following url:
/var/www/html/new-path
Sample request looks something like:
https://host-name/old-path/param1/param2/param3/param4
and rewrite rule should just replace old-path with the new-path.
Can anyone give me some clues about what am I doing wrong? and how can I fix it?
Thanks in advance!
If your are aiming for 'hiding' the rewrite, try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/old-path/(.*)$ /new-path/$1 [P,L]
</IfModule>
And if you REALLY want to extract the hostname:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} (.*)
RewriteRule ^/old-path/(.*)$ http://%1/new-path/$1 [P,L]
</IfModule>
Since %1 references the first bracket of RewriteCond-line...
If you want customers to be rewritten to the new URI and see that:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/old-path/(.*)$ https://host-name/new-path/$1 [R=301,L]
</IfModule>
or in short, and without even using a rewrite:
RedirectMatch ^/old-path/(.*)$ /new-path/$1
To avoid 'https://host-name/' change your DocumentRoot to the parent-directory of 'old-path' and especially 'new-path'.

Enable human readable URL's in .htaccess

Preface: Yes this question seems like duplicated, and I found related questions, but answers from there didnt help to me. :(
Hello, I want to add human readable URL's support for my PHP project. For now my URL quesry string looks like:
index.php?url=main/index
I would like to make it looks like:
index.php/main/index
I read following articles:
Stackoverflow
Cheatsheet
Stackoverflow
Stackoverflow
But when I do this:
var_dump($_GET['url']); // get empty array
, get empty array like no url parameter added.
My current .htaccess:
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ index.php?url=$1 [NC]
Can somebody help me please? Thanks!
URL: http://domain.com/index.php/controller/action
Rewritten URL: http://domain.com/index.php?url=controller/action
.htaccess
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^index.php/(.*)$ /index.php?url=$1 [L,QSA]
Explanation:
The .* in the pattern ^index.php/(.*)$ matches everything after index.php/ on the incoming URL. The parentheses helps to capture the part as variable $1, which is then added at the end of the substitution URL /index.php?url= + $1.
[L, QSA]:
L ignore other rewrite rules, if this fits.
QSA means query string append.
You have
index.php?url=main/index
Yuo want to rewrite (and then user redirect) to this
index.php/main/index
What about trying this?
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^url=([a-z]+)/([a-z]+)$
RewriteRule ^.*$ http://mydomain.site/index.php/%1/%2 [R=302,L]
R=301 or 302 depend on your need
On this example i assumed that on the original url there are only a-z chars.
Just to clarify (due the fact generally the process is inverse) on this way the users will be redirect, this rules does not convert links on your page.
In line
RewriteRule ^(.*)$ index.php?url=$1 [NC] the (.*) matches the part of the url up to '?' where the query begins.
To use the values passed in the query you need the QSA flag. Means Query String Append.
If Your URL : www.abcd.com/index.php/abcd/...
.htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Try the below code, Only considered for index.php you can replace it as per your requirement. Let me know if you need further assistance.
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine ON
RewriteBase /
RewriteRule ^(index\.php)/([0-9a-zA-Z\/_-]{1,99}) index.php?url=$2
What worked for me is:-
<?php var_dump($_GET); ?> this is the only thing I did on url http://localhost/baba.php/abcd . and original .htaccess file
DirectoryIndex baba.php
RewriteEngine ON
RewriteBase /
RewriteRule ^(baba\.php)*/([0-9a-zA-Z\/_-]{1,99}) baba.php?url=$2
For Magento 2.4 Magento will always drop any GET parameters from the URL in a htaccess rewrite. The only way I found to make it work was to create a rewrite module. The setup is here: https://magento.stackexchange.com/a/158811/109113

replace a string in url in .htaccess

I would like to redirect www.hostname.com/some path/?cpao=12 to www.hostname.com/some path/?kmas=12.
Essentially replacing the word cpao with kmas and keeping everything else the same.
Any help will be appreciated.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^cpao=([^&]*) [NC]
RewriteRule ^ %{REQUEST_URI}?kmas=%1 [R,L]
RewriteRule ^hostname.com/somepath/\?cpao([0-9]+)$ hostname.com/somepath/\?kmas=$1
you use regex in htaccess $1 is the numbers that was catched in group 1

How would I write a RewriteRule to make redirection

I want to rewrite the html and xml files and directories to the pagename query string using .htaccess file. like this :
mysubsite/category_1/category_2/
mysubsite/category_1/category_2/file1.html
mysubsite/category_1/category_2/file2.152.html
index.php?pagename=category_1/category_2/
index.php?pagename=category_1/category_2/file1.html
index.php?pagename=category_1/category_2/file1.152.html
Thanks for you help.
You have
mysubsite/category_1/category_2/ mysubsite/category_1/category_2/file1.html mysubsite/category_1/category_2/file2.152.html
and
index.php?pagename=category_1/category_2/ index.php?pagename=category_1/category_2/file1.html index.php?pagename=category_1/category_2/file1.152.html
And I'm supposing that you meant:
mysubsite/category_1/category_2/
mysubsite/category_1/category_2/file1.html
mysubsite/category_1/category_2/file2.152.html
and
index.php?pagename=category_1/category_2/
index.php?pagename=category_1/category_2/file1.html
index.php?pagename=category_1/category_2/file1.152.html
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^mysubsite/(.*) index.php?q=$1 [NC,L,QSA]
</IfModule>

Rewriting a number based URL using .htaccess RewriteRule

How can I rewrite a simple number based URL to a sub folder?
I want http://mysite.com/123 to redirect to http://mysite.com/en/123.
The number could be anything from 1 - 9999. My attempt in htaccess was:
RewriteRule ^([0-9]+)$ /en/$1/ [R]
But that doesn't work.
your syntax is right, I just remove slash in the end of line and it works:
RewriteRule ^([0-9]+)$ /en/$1
Make sure that this 3 lines are writen in your HtAccess File (sorry for mybad english)
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
##
## Now the rewrite
RewriteRule ^([0-9]+)$ ./en/$1