logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Apache View previous topic :: View next topic
Reply to topic   Topic: RewriteRule not working
Author
david.livelsberger



Joined: 18 Jan 2017
Posts: 8
Location: Detroit, Michigan, USA

PostPosted: Tue 17 Apr '18 19:24    Post subject: RewriteRule not working Reply with quote

I have been asked to add the following rewrite to a website:
http://www.website.com/neighborhoods/columbia-street
needs to be redirected to
http://www.website.com

I have tried the following. Neither works.
RewriteCond %{HTTP_HOST} ^(www\.)?website\.com/neighborhoods/columbia-street$ [NC]
RewriteRule ^(.*) http://www.website.com/$1 [R=301,L]

RewriteCond %{DOCUMENT_ROOT}/neighborhoods/columbia-street/%{REQUEST_URI} -f
RewriteRule ^(.+) %{DOCUMENT_ROOT}/$1 [L]

What am I doing wrong? Can anybody please help me to get this redirect to work?
Thank you in advance for your help!
Back to top
mraddi



Joined: 27 Jun 2016
Posts: 149
Location: Schömberg, Baden-Württemberg, Germany

PostPosted: Wed 18 Apr '18 11:04    Post subject: Reply with quote

Hello David,

Your first try will not work as you are checking agains the HOST-header and not against the complete URL.

The second try is confusing me:
It look similar to the following line from http://httpd.apache.org/docs/current/mod/mod_rewrite.html
RewriteCond /var/www/%{REQUEST_URI} !-f
There it is checked that the requested ressource is available on the filesystem. But in my eyes the part "neighborhoods/columbia-street/" is somehow wrong.

Please try the following config:
RewriteCond %{REQUEST_URI} ^/neighborhoods/columbia-street
RewriteRule ^(.*) / [R=302,L]
which redirects all requests to /neighborhoods/columbia-street and ressources within to /.

Or use just the following One-Liner:
RewriteRule "^neighborhoods/columbia-street(.*)" "/$1" [R=302,L]
to redirect all requests to /neightborhoods/columbia-street two directories up:
/neighborhoods/columbia-street => /
/neighborhoods/columbia-street/no1 => /no1
/neighborhoods/columbia-street/no2/flat3 => /no2/flat3

Greetings
Matthias Smile
Back to top
david.livelsberger



Joined: 18 Jan 2017
Posts: 8
Location: Detroit, Michigan, USA

PostPosted: Wed 18 Apr '18 13:43    Post subject: Reply with quote

Matthias,
vielen Dank!
I used your "one-liner" and it worked perfectly!

mraddi wrote:
Hello David,

Your first try will not work as you are checking agains the HOST-header and not against the complete URL.

The second try is confusing me:
It look similar to the following line from http://httpd.apache.org/docs/current/mod/mod_rewrite.html
RewriteCond /var/www/%{REQUEST_URI} !-f
There it is checked that the requested ressource is available on the filesystem. But in my eyes the part "neighborhoods/columbia-street/" is somehow wrong.

Please try the following config:
RewriteCond %{REQUEST_URI} ^/neighborhoods/columbia-street
RewriteRule ^(.*) / [R=302,L]
which redirects all requests to /neighborhoods/columbia-street and ressources within to /.

Or use just the following One-Liner:
RewriteRule "^neighborhoods/columbia-street(.*)" "/$1" [R=302,L]
to redirect all requests to /neightborhoods/columbia-street two directories up:
/neighborhoods/columbia-street => /
/neighborhoods/columbia-street/no1 => /no1
/neighborhoods/columbia-street/no2/flat3 => /no2/flat3

Greetings
Matthias Smile
Back to top


Reply to topic   Topic: RewriteRule not working View previous topic :: View next topic
Post new topic   Forum Index -> Apache