RewriteRule for character after slash - .htaccess

I want to apply RewriteRule for specific pages in my website.
Original link :
www.abc.com/category/foo.html
How will I apply RewriteRule to redirect to homepage (www.abc.com) if any link contains any character (not number) after the slash (category/_123etcetc) "www.abc.com/category/" ?
For example;
i want to redirect www.abc.com/category/apple.html or www.abc.com/category/key.html to homepage but i don't want to redirect www.abc.com/category/1-apple.html or www.abc.com/category/123-key.html

Related

How to rewrite URLs that cause duplicate content?

I have a lot of links with a double URL structure. Basically, I need a .htaccess rule that redirects all URLs that start /de (for German language) to the same URL with only a /.
example.com/de/Shop/Tradition/Jagd-Forst/
to
example.com/Shop/Tradition/Jagd-Forst/
Your rewrite rule should match starting with /de/ (with an optional strating slash, capture everything afterwards with (.*) and redirect to the capture group ($1) with a 301 permanent redirect:
RewriteEngine on
RewriteRule ^/?de/(.*) /$1 [R=301,L]

Page Redirect 301 using htaccess

I want to redirect /event/path1/{id}/{some_name} to /event/path2/{id},
I want to redirect old page to the new one and remove the last segment of the url,
I have done with this but not removing the last segment of the url, this is what .htaccess
RewriteRule ^event/path1/(.*) /event/path2/$1 [L,R=301]
You're using .* in your regex that matches everything after /event/path1/ thus resulting in wrong target URL.
You may use:
RewriteRule ^event/path1/([^/]+)/[^/]+/?$ /event/path2/$1? [L,R=301,NC]
Here [^/]+ will match 1 or more of any non-slash character.
? at the end of the target will strip off any previous query string.

How to remove specific character from url using htaccess

We want to remove specific characters from the url using htaccess.
for example http://www.example.com/#/music.php which is redirect to http://www.example.com/music.php using htaccess rules.
we used the following code but its not working
RewriteEngine On
RewriteRule ^\.html$ /#/music.php [L]

How to redirect from URL with a trailing dot?

Having a Drupal here. In that Drupal is a folder with another Drupal. So in another Drupal's .htacces the base was rewritten like this Rewrite Base /another-drupal and everything's fine.
Now, printed invitations were send to customers to look at the another Drupal via its URL adrupal.com/another-drupal. And like right now the URL stands at the end of a sentence and the dot looks like it is part of the URL. How do I redirect all users who entered the URL with a trailing dot to the correct URL without the trailing dot? And in which .htaccess do I have to do that? The parent's Drupal .htacess? Or in the another Drupal's .htaccess?
I allready tried that approach: Redirect url to home page with trailing dots, but it didn't work. Is it maybe because in that example they have two dots?
Try:
RewriteCond %{THE_REQUEST} \ /([^\?\ .]*)\.(?:\?|\ |$)
RewriteRule ^ /%1 [L,R=301]
Looks like this simple rule is all you need:
RewriteRule ^(.+?)\.$ $1 [L,R=301]
This will remove trailing period (dot) from any URL.

htaccess redirect urls to specific urls

I have Old URLs in my site and it like this:
http://'www.example.com/newsroom/press-release/item/949-ItemAlias.html
I want to redirect it to :
http://'www.example.com/press/ItemAlias.html
I used this sentence in .htaccess:
RewriteRule ^newsroom/press-release/item/(.*)$ /press/$1 [R=301,NC,L]
But this sentence redirect the URLs to http://'www.example.com/press/949-ItemAlias.html Of course the (949-) is example, I want to remove the number and dash when i redirect the URLs. What is the changes i have to do in .htaccess?
Your regex should match the number, but not include it in the ()'s. ex:
RewriteRule ^newsroom/press-release/item/[0-9]*-(.*)$ /press/$1 [R=301,NC,L]
The [0-9]* matches an arbitrary length number, followed by the -. Only what's matched parens will be included in $1.

Resources