.htaccess Redirections with custom parameters - regex

I want to redirect the following link using .htaccess file
Old Url : localhost/info.php?butter_cake-cid111.html
To
New Url : localhost/butter_cake/cid/111.html
Thanks in advance.

You can use this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^([^-]+)-([a-z]+)(\d+\.html)$ [NC]
RewriteRule ^info\.php$ /%1/%2/%3? [L,NC,R=302]
RewriteCond is matching and capturing all the values that are to be used later in target URL. ? in the end will strip off previous query string.

Related

Replace URL parameter separator with .htaccess

A WP plugin is generating URLs with ? parameter separator, instead of &
Something like: https://yourdomain.com/?quiz=lesson-1-quiz-1?id=1260
Is it possible to leave first ? as it is and replace all other with & using .htaccess?
The URL should be: https://yourdomain.com/?quiz=lesson-1-quiz-1&id=1260
With your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^([^?]*)\?(.*)$
RewriteRule ^ %{REQUEST_URI}?%1&%2 [R=301,NE,L]
You may use this rule as your topmost rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} (\?[^?]*)\?(\S*)\sHTTP [NC]
RewriteRule ^ %{REQUEST_URI}%1&%2 [L,R=302,NE]
# all other rules go below this line
Once you've tested this rule, don't forget to change 302 to 301.

I need to rewrite Malformed URL to remove everything after .html

I have malformed URLs like
content/abc/na/abc/en_us/find.html/bridge/bridge/bridge/ddc.html?dealerUrl=http://WWW.ssc.NET&modelYearCode=01714
I want everything after the first .html to be removed.
Can anyone help me with this?
You can use this rule as your first rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.+?\.html).+$ [NC]
RewriteRule ^ %1? [L,NE,R=301]
Trailing ? will strip query string.

RewriteRule when there is ?id=

I try to write a RewriteRule in the .htaccess but I have problems
I try to redirect from:
blog/entrada.php?id=2
To:
/blog/3D-touch
This is one of the multiple things I tried and does not work:
RewriteRule ^blog\/entrada\.php\?id=2$ /blog/3D-touch [L,R=301]
What is wrong with my Rule. How to redirect effectively?
Thanks
Querystring is not part of match in RewriteRule directive, to redirect query strings, you need to use RewriteCond one of the following options :
option 1
RewriteEngine on
RewriteCond %{THE_REQUEST} /blog/entrada\.php\?id=2 [NC]
RewriteRule ^ /blog/3D-touch? [NC,L,R]
option 2
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=2$ [NC]
RewriteRule ^blog/entrada\.php$ /blog/3D-touch? [NC,L,R]
We use an empty question mark ? at the end of the target url to discard the old query strings, otherwise these query strings get appened to the target url by default.
Change the R to R=301 if you want to make the redirection permanent.
use from this code
RewriteRule blog/(.*) blog/entrada.php?id=$1
this code will redirect all urls which have blog/ to blog/entrada.php and put after value of blog/ to $_GET['id']
you should have following code n the top location of your htaccess file
Options +FollowSymLinks
RewriteEngine on

Rewrite search page query URL with htaccess

I want when search form submitted, the URL be like this :
localhost/xampp/external/proj3/search/searchquery
Instead of this:
localhost/xampp/external/proj3/tools/search/search.php?query=searchquery
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /xampp/external/proj3
# external redirect from action URL to pretty one
RewriteCond %{THE_REQUEST} /search(?:\.php)?\?query=([^\s&]+) [NC]
RewriteRule ^ search/%1? [R=302,L,NE]
# internal forward from pretty URL to actual URL
RewriteRule ^search/([^/.]+)/?$ search.php?query=$1 [L,QSA,NC]
Create a .htaccess file in your document root. And put this:
RewriteEngine On
RewriteRule ^search/([a-zA-Z0-9]+)/?$ \
search.php?query=$1 \
[NC,L]
Note: Be careful with the regular expression. Filter it cautiously.

How to RewriteRule WITH and WITHOUT parameters in htaccess?

I want to route all .php and .html files to a certain php file wether they have URL parameters or not.
Example:
www.mydomain.com/mysite.php?par1=test
www.mydomain.com/intro.html?nopar=1
Idea:
RewriteRule \.(?:php|html)$ work.php
You're pretty close, just add a condition to avoid rewriting if request is already for work.php:
RewriteEngine On
RewriteCond %{REQUEST_URI} !/work\.php$ [NC]
RewriteRule \.(?:php|html)$ work.php [L,NC]
Note that query string is automatically carried over to target URI.