replace a string in url in .htaccess - apache

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

Related

Replacing Strings with .htaccess in URL

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

Htaccess not getting 'GET' variables even with QSA flags

I can't seem to get my 'GET' variables passing through with my htaccess code. Here it is below:
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,L]
RewriteRule ^/(.*)/(.*)/(.*)$ v.php?shareid=$1&filename=$2 [QSA, L]
</IfModule>
I have v.php in the root, not in any folder and I am trying to achieve the following URL:
http://localhost/v/ubgvfmrsazwxoyp/Screen%20Shot%202015-11-05%20at%2020.51.45.png
where ubgvfmrsazwxoyp is the shareid and Screen%20Shot%202015-11-05%20at%2020.51.45.png the file name.
None are getting registered when page is loaded.
Note: I also want to keep .php extensions hidden.
Any ideas?
RewriteEngine On
RewriteBase /
RewriteRule ^v/([^/]+)/(.*)/?$ v.php?shareid=$1&filename=$2 [L]
You were previously getting v as the shareid and ubgvfmrsazwxoyp as the filename.
NOTE: The above code is specifically for an .htaccess file inside the server root directory. If the rewrite rules are going in the vhost config or the server config file, you'll need to use:
RewriteRule ^/v/([^/]+)/(.*)/?$ v.php?shareid=$1&filename=$2 [L]

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

dynamic file extensions htaccess redirection

Hi I have to redirect current PHP dynamic pages:
http://www.sample.com/page/2/?wpec_spao=include-inserting-tool
So I want everything that has: "http://www.sample.com/page/2/?wpec_spao=" to be redirected regardless of remaining part of the link (that remaining part is dynamically generated) to:
http://www.sample.com/site-moved-page/
Any help on how to do this?
Add this to your .htaccess in your DocumentRoot.
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI}?%{QUERY_STRING} ^/page/2/\?wpec_spao= [NC]
RewriteRule ^ http://www.sample.com/site-moved-page/? [R=301,L]

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