How to transform / remap URLs using .htaccess - .htaccess

I want a redirection in my website.
The old url looks like this :
http://www.example.com/Prem_Ratan_Dhan_Payo/Prem_Leela.html
Now we have to 301 redirect it to
http://example.com/songs/prem-leela/
In total I have to
Check if the url end with .html then we will redirect it other wise let it pass.
remove .html from end.
make all letters in lower case.
change underscore to hyphen.
change second segment to 'songs'.
Please help me.

This should get you started:
RewriteEngine On
# Replace underscores with hyphens in all requests ending in .html.
# The rule replaces one char at a time and is repeated until no matches are found.
RewriteCond "%{QUERY_STRING}" "\.html$" [NC]
RewriteRule ^([^_]*)_(.*) $1-$2 [N]
# Capture the file name without extension inside braces
# Use a lower-case mapping on the file name.
# Destination is /songs/filename
RewriteMap lc int:tolower
RewriteRule ^.*/(.*)\.html$ /songs/${lc:$1} [NC, R=301]

Related

How can I Rewrite Query String to Path and remove %20 in url with dash (-)? [duplicate]

So here's my problem. I took over a site that has has a bunch of pages indexed that have %20 indexed in Google. This is simply because the person decided to just use the tag name as the title and url slug. So, the urls were something like this:
http://www.test.com/tag/bob%20hope
http://www.test.com/tag/bob%20hope%20is%20funny
I have added a new field for the url slug and string replaced all spaces with dashes. While I have no problem linking to these new pages and getting the data, I need to 301 redirect the old URLs to the new URLs, which would be something like:
http://www.test.com/tag/bob-hope
http://www.test.com/tag/bob-hope-is-funny
So, it needs to be able to account for multiple spaces. Any questions? :)
Use these rules in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
# keep replacing space to hyphen until there is no space use internal rewrite
RewriteRule ^([^\s%20]*)[\s%20]+(.*)$ $1-$2 [E=NOSPACE:1]
# when there is no space make an external redirection
RewriteCond %{ENV:NOSPACE} =1
RewriteRule ^([^\s%20]+)$ $1 [R=301,L]
This will replace all space characters (\s or %20) to hyphen -
So a URI of /tag/bob%20hope%20is%20funny will become /tag/bob-hope-is-funny with 301
Brief Explanation: If there are more than 1 space in URI then 1st RewriteRule is fired recursively replacing each space character with hyphen - until there is no space left. This rule will only rewrite internally.
Once no space is left 2nd RewriteRule is fired which just uses a 301 redirect to the converted URI.
Building on #anhubhava's answer, it's close, but that will also match %,2 or 0 in the URL, and it can cause a loop on apache 2.2 if you don't use the DPI parameter. The full script should look like this:
Options FollowSymlinks MultiViews
RewriteEngine on
RewriteBase /
# keep replacing space to hyphen until there is no space use internal rewrite
RewriteRule ^([^\s%20]*)(?:\s|%20)+(.*)$ $1-$2 [N,E=NOSPACE:1,DPI]
# when there is no space make an external redirection
RewriteCond %{ENV:NOSPACE} =1
RewriteRule ^([^\s%20]+)$ $1 [R=301,L]
I've also added the N (Next) parameter as this then forces the rules to be re-evaluated from the start straight after this rule if it matches. If this isn't there, you can get problems if you're using apache as a reverse proxy as it's unlikely that it'll get to the end of the rewrites before something else happens.

how to make a permanent redirect via htaccess?

Google has indexed some URLs with empty value - space - at the end of each URL, I want to make a permanent redirect via .htaccess here is my code but it did not redirecting :
RewriteRule ^/profile/userx/([a-zA-Z])+/[a-zA-Z]+/[a-zA-Z]/'%20'$ https://mywebsite.net/profile/userx/([a-zA-Z]+)/[a-zA-Z]+/[a-zA-Z]/ [R=301,L]
How to say any URL with space at the end redirect it to that one without space?
You may use this rule as your topmost rule to remove 1+ whitespace from the end of URL:
RewriteEngine On
RewriteRule ^(.*)\s+$ /$1 [L,NE,R=301]
# other rules go below this line
\s+$ matches 1+ whitespace at the end of a URI and .* matches anything before whitespaces.

Error with .htaccess

I need to open my file banclub.html on address http://site.ru/products/love-is
I was create .htaccess file:
RewriteEngine on
RewriteRule ^/banclub.html$ /products/love-is
What's wrong?
Is the incoming URL http://site.ru/products/love-is, and you want to send it to http://site.ru/banclub.html? Or vice-versa? Assuming the first case:
RewriteEngine On
RewriteRule ^products/love-is$ /banclub.html
Note that the RewriteRule has already stripped off the leading /. This rule will pass along any Query String in the URI (?var=value&var=value etc.) to the new URI. You can suppress that by adding just a ? after .html . If "love-is" is a directory, and the user might have a trailing /, change that line to
RewriteRule ^products/love-is/?$ /banclub.html
If you want to tell search engines and visitors to update their indexes and bookmarks, add [R=301] at the end of the RewriteRule line.

.htaccess to remove arbitrary text in filenames

I've got a client who uploads thousands of images with names like 1057_1.jpg , 1057_2.jpg, 1083_1H.jpg etc - always a number, an underscore, a number and an optional letter. The CMS uses these to link them to relevant entries.
For SEO reasons we want those image filenames to contain some keywords taken from the CMS. So they would become, say, 1057_1-some-keywords-here.jpg. Is there a way, with .htaccess, to keep the filenames the same, but redirect 1057_1-any-arbitrary-words.jpg to 1057_1.jpg? Basically to remove everything from the first dash up to the dot?
Thanks for your help - I must learn htaccess properly sometime but need to find a quick solution for now!
You may try this:
RewriteEngine On
RewriteBase /
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /([^\-]+)-.+\.([^/]+)/?
RewriteRule .* %1.%2 [R=301,L]
Redirects permanently any URL like:
http://example.com/1057_1-anything.jpg or
http://example.com/any/number/of/folders/1057_1-anything.jpg
To:
http://example.com/1057_1.jpg or
http://example.com/any/number/of/folders/1057_1.jpg
Effectively removing -anything from the last string in the URL-path.
The image name hast to be the last string in the URL-path, for the rule-set to work.
For a silent mapping, remove R=301 from [R=301,L].
UPDATE:
anything did not include the period as it was used to determine the end of the name and the start of the extension. However, I modified the rule-set to remove also any number of periods in anything except the last one, according to the OP requirement in previous comment.

htaccess redirect fake folder structure

Trying to create fake folder structure.
Essentially what I want is any request to http://www.example.com/** (where ** equals any two letters) to redirect to http://www.example.com/folder/index.php?var=** (where ** equals the two letters from the first link)
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^/(.*)$ http://www.example.com/folder/index.php?var=$1
This is what I have right now, but it isn't working. Any ideas?
Remove the leading / from the RewriteRule. The expression matched on the left side should not begin with / as a REQUEST_URI would. You can also omit the entire http://example.com from the right side, since you are rewriting on the same domain.
RewriteEngine On
# Rewrite 2 characters into var
RewriteRule ^(.{2})$ folder/index.php?var=$1 [L,R]
If it should match only alpha chars, it is recommended to limit the expression further (beyond .{2}, which matches 2 of any character)
# Only rewrite on exactly 2 alpha chars...
RewriteRule ^([A-Za-z]{2})$ folder/index.php?var=$1 [L,R]

Resources