Remove last Path from URL using .htaccess - regex

I need to create a RewriteRule on my .htaccess file which removes the Path ("page-2") if it's included at the end of the URL. So for example:
http://myhost.com/path/page-2
should redirect to:
http://myhost.com/path
I've found a similar solution on SO:
RewriteEngine On
RewriteRule ^([^/.]+)/?$ /$1/page-2/ [L]
However it does not work for me. No redirection happens.
Any help?

You need to have this rule:
RewriteEngine On
RewriteRule ^(.+?)/page-2/?$ /$1 [L,NC,R=301]

Related

redirect and remove slug if found anywhere using .htaccess

I want to remove index.php/
example.com/index.php/apple example.com/fruits/index.php/apple
example.com/fuits/apple/index.php/ripen example.com/index.php
in any of the above cases, I need the url to be redirected by removing index.php/
I tried
RewriteEngine on
RewriteBase /
RewriteRule ^index.php/(.*) /$1 [R=301,L]
But it only works for the first case from the given urls above.
With your shown samples, please try following htaccess rules file. Make sure to keep your file in root.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Doing external redirect for index.php here.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php/?$ /? [R=301,L]
##Doing external redirect to remove index.php here.
RewriteRule ^(.*/)?index\.php/(.*) /$1$2? [R=301,L]
##Handling home page with internal rewrite here.
RewriteRule ^/?$ index.php [L]

Silent Rewrite Rule

I want to make this redirection:
www.domain.com/es/j/j1.html => www.domain.com/j/j1.html?lang=es
To do this I have created an .htacces in the folder /es/j with the following code:
RewriteEngine On
RewriteRule ^j(.*)$ www.domain.com/j/j$1?lang=es
It works, but the thing is that I need to do it "silently", I mean, the url in the browser must show www.domain.com/es/j/j1.html instead of www.domain.com/j/j1.html?lang=es
How can I do that?
If you want to have a silent rewrite then use this code.
In root .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^es/(j/.+?\.html)$ /$1?lang=es [L,NC,QSA]

Rewrite url for subdirectory in .htaccess

I moved wordpress files from root to a subdirectory. There are some files uploaded in wp-content/uploads folder. When accessing those files directly, I get a 404 error.
I need to rewrite the url only for the wp-content/uploads through .htaccess
What I need is to redirect from
http://example.com/wp-content/uploads/2012/09/report.pdf
to
http://example.com/wp_sub/wp-content/uploads/2012/09/report.pdf
This is what I've tried
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(.*)$ http://www.example.com/wp_sub/$1 [R=301,QSA,L]
This adds a subdirectory wp_sub to every link.
I need to add a subdirectory "wp_sub" only for wp-content/uploads links.
What am I missing?
Let's see what
RewriteRule ^(.*)$ http://www.example.com/wp_sub/$1
means:
You rewrite everything (^(.*)$) to http://www.example.com/wp_sub/$1.
Well, you don't want to rewrite everything, do you? :)
So we have to restrict this to only affect the wp-content folder:
RewriteRule ^(wp-content/.*)$ http://www.example.com/wp_sub/$1 [R=301,QSA,L]
Place this rule in your /wp-content/uploads/.htaccess file:
RewriteEngine On
RewriteBase /wp-content/uploads/
RewriteRule ^(.*)$ /wp_sub/$1 [L,R]
You can use the code :
RewriteRule ^wp-content/uploads/(.*)$ http://www.example.com/wp_sub/wp-content/uploads/$1 [R=301,L]

htaccess redirect one folder to another on same domain if request_uri condition false

I'd like to redirect the visitors if they visit the old not working URL example.com/english/* to example.com/german/* but not when they're visiting example.com/
This code in .htaccess is not working:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/german/(.*)$ [NC]
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^/english/(.*)$ /german/$1 [R=302,NC,L]
I searched throrough the apache docs and google but nothing does the same.
Where's the problem?
Replace your code with this:
RewriteEngine on
RewriteRule ^english/(.*)$ /german/$1 [R=302,NC,L]
RewriteRule doesn't match leading slash in .htaccess.

Mod Rewrite and 404 Error

At the moment the users of my website can access files with the following url-schema:
http://www.example.com/dwl/download.php?file=/files/index.rar
Now i want that they can access it with the following schema:
http://www.example.com/dwl/content/files/index.rar
The download.php file is within the dwl directory.
I tried the following .htaccess:
RewriteEngine On
RewriteRule ^file/([^/]*)$ download.php?file=$1 [L]
But when i try it, i get a 404-Error.
Can anybody please help me to write the correct .htaccess-Entry?
If you want to redirect from http://www.example.com/dwl/download.php?file=/files/index.rar to http://www.example.com/dwl/content/files/index.rar, you need this:
RewriteRule ^/dwl/download.php?file=/(.*) /dwl/content/$1 [L]
I think the code you have to use is the following :
RewriteEngine On
RewriteRule ^/dwl/content(/files/[^/]*)$ /dwl/download.php?file=$1
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT/dwl directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /dwl/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+dwl/download\.php\?file=([^\s&]+) [NC]
RewriteRule ^ content/%1? [R=301,L]
RewriteRule ^content/(.+)$ download.php?file=$1 [L,QSA,NC]