How can i customize my url using htaccess file - .htaccess

#how can i change this url? #
http://www.domain.in/product-category.php?id=3&name=house-hold&type=top-category
to
http://www.domain.in/product-category/house-hold
using .htaccess.

This might help you:
RewriteCond %{HTTP_HOST} ^www.domain.in
RewriteCond %{QUERY_STRING} name=(.*)&type
RewriteRule (.*).php /$1/%1?
Line 1 : we check the domain
Line 2 : we save the parameter name in a var (%1)
Line 3 : we rewrite rule with $1 (`REQUEST_URI` before `.php`) and %1.
https://htaccess.madewithlove.com?share=71f3a8dd-6b81-4620-b02c-2ae47297c8fc

Related

htaccess url redirects with url contains = something

I want to create some userfriendly new redirects for my website.
My internal search pattern is the following : website.com/search/stuff
I want to add in my htaccess a rule that would redirect : /search=stuff
to : /search/stuff
I was trying the following code into my htaccess file :
RewriteCond %{QUERY_STRING} \\search=([^&]+) [NC]
RewriteRule ^$ /search/%1/? [NC,R,L]
But that doesnt seem to work... any ideas of where I may be wrong?
Thanks !

.htaccess redirect to subdomain with regex

I want to 301 redirect a specific path to another subdomain with htaccess
I want :
A : http://www.example.org/markets/access/id/56
B : http://www.example.org/markets/market/id/56
to redirect to :
A : https://archive.example.org/markets/access/id/56
B : https://archive.example.org/markets/market/id/56
I have tried this but get an error 500:
RewriteCond http://www.example.org/markets/(.+?)
RewriteRule https://archive.example.org/markets/$1 [R=301,L]
Thank you
Based on your shown samples, could you please try following. Please make sure you put these rules at the top of your .htaccess file.
Also make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.(example\.org)$ [NC]
RewriteRule ^ https://archive.%1%{REQUEST_URI} [NE,R=301,L]

How can I redirect url with .htaccess like instagram?

If the link does not have any value, redirect to index.php.
Example;
www.mydomain.com/ --> index.php
If the link is www.example.com/post/value, redirect to post.php.
Example;
www.mydomain.com/post/cDfS58Q --> post.php
If the link only consists of value, redirect to profile.php.
Example;
www.mydomain.com/jhon.34 --> profile.php
My test:
RewriteRule ^post/([0-9a-zA-Z-_/.]+)$ post.php?$1 [QSA,L]
RewriteRule ^([0-9a-zA-Z-_/.]+)$ profile.php?$1 [QSA,L]
but they all go to post.php.
Set the rewrite rules for each scenario except for the link that does not have any value. The default file that opens when visiting a website is the index file so you don't need to explicitly set it.
Go from longest URI to smallest because the condition of the smaller URI usually satisfies the conditions for the bigger URI.
Try somethig like this:
# Mod Rewrite setup
Options +FollowSymlinks
RewriteEngine on
AddDefaultCharset UTF-8
# First enter the one with the longest URI
RewriteCond %{REQUEST_URI} ^/?post/([0-9a-zA-Z-_/.]+)/?$
RewriteRule ^(.*)$ /post.php?data=%1 [NC,L]
# Then the one with only value
RewriteCond %{REQUEST_URI} ^/?([0-9a-zA-Z-_/.]+)/?$
RewriteRule ^(.*)$ /profile.php?data=%1 [NC,L]
I did it in a different way ;
htaccess
RewriteRule ^p/(.*)$ post.php?$1 [QSA,L]
RewriteRule ^([0-9a-zA-Z]+)$ index.php?$1 [QSA]
RewriteRule ^([0-9a-zA-Z]+)/$ index.php?$1 [QSA]
index.php
<?php
if (!empty($_SERVER['QUERY_STRING'])) {
//Profile.php will be included here.
echo "You are in profile now.";
}else {
//Homepage.php will be included here.
echo "You are in profile now.";
}
?>
Url : www.localhost.com/adsasd
Output : You are in profile now.
Url : www.localhost.com/
Output : You are in homepage now.

htaccess mod rewrite changes http://www to http:/www

I want to replace calls like this:
www.mysite.com/sub/file.php?param1=x&param2=http://www.someurl.com
with:
www.mysite.com/sub/param1/param2
Param 1 is an integer number Param 2 is a url
I wrote this rewrite rule in htaccess:
RewriteCond %{REQUEST_URI} \/sub\/
RewriteRule sub\/([0-9]+)\/(.*)$ sub\/file.php?param1=$2&param2=$1 [L]
Unfortunately param2 (the URL) starts with http:/www.someurl.com instead of http://www.someurl.com (note the single slash).
Any idea what causes it? When I call the same file with same parameters in the format www.mysite.com/sub/file.php?param1=x&param2=http://www.someurl.com , param2 does appear OK so it must be something with the rewrite rule.
You need to grab the value from THE_REQUEST:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /sub/[0-9]+/([^?\ ]+)
RewriteRule ^sub/([0-9]+)/ sub/file.php?param1=$1&param2=%1 [L]

htaccess subdomain redirct with last url parameter

I want to write a .htaccess file for redirecting my subdomains and URL's last variable to a new location. Here is what I want to do:
http(s)://abc.example.com/books
I want my internal URL to be like:
http://example.com/?name=abc&type=books
I have already gotten the subdomain redirect to work but I am not able to do subdomain with variable in last part of URL.
How can I accomplish this?
This should do what you want:
RewriteCond %{HTTP_HOST} ^(.+).example.com
RewriteRule ^(.*)% http://example.com/?name=%1&type=$1 [R,L]
The "%1" means use the first capture group from the RewriteCond above.
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com
RewriteRule ^([^/]*)$ http://example.com/?name=%1&type=$1 [R,L]
The "%1" means use the first capture group from the RewriteCond, while $1 is the first capturing group in the rule itself.
In your example %1 will be "abc" and $1 will be "book"
[^/]* means "match every character not being a slash, 0 or more times"

Resources