LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 01-30-2012, 07:17 PM   #1
jnyunt
LQ Newbie
 
Registered: Jun 2008
Posts: 26

Rep: Reputation: 0
Help with removing %20 from URL with mod_rewrite


Hi all,

Pulling my hair out(whatever left of it)) .
I got some articles linking to my site like
<a href="http://www.blahblah.com/%20">blah</a> , well of course, Gbot is giving craw error. So I was trying to rewrite in .htaccess

RewriteRule ^/\%20$ / [R=301]

and other different things. But still didn't work out. So please expert help is needed.

Thanks in advance
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 01-31-2012, 01:47 AM   #2
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Hi,

Try this:
Code:
RewriteEngine On

RewriteCond %{REQUEST_URI} "^/ "
RewriteRule (.*) / [R=301]
Regards
 
Old 01-31-2012, 04:45 AM   #3
boloo
LQ Newbie
 
Registered: Jan 2012
Posts: 15

Rep: Reputation: Disabled
Hi bathory,

Sorry for name changed.

I tried with

RewriteEngine On

RewriteCond %{REQUEST_URI} "^/ "
RewriteRule (.*) / [R=301]


but it didn't work. In httpd access log, it showed "GET /%20 HTTP/1.1" 404. I also tried with
RewriteCond %{REQUEST_URI} ^/\%20 , but result was the same..

any thought!!
 
Old 01-31-2012, 05:14 AM   #4
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
It works here (see the 301 response):
Quote:
x.x.x.x - - [31/Jan/2012:13:06:10 +0200] "GET /%20 HTTP/1.1" 301 228 "-"
Are you sure you leave a blank space after the slash (/) in the RewriteCond? This is because apache turns %20 into a space.
You can check error_log to see why you get the 404 error.
 
Old 01-31-2012, 05:50 AM   #5
boloo
LQ Newbie
 
Registered: Jan 2012
Posts: 15

Rep: Reputation: Disabled
Hi Bathory,

Thanks again, below is what I got in .htaccess

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} "^/ "
RewriteRule (.*) / [R=301]

#####301 redirect for url doesn't exist
###/index.php/it-service ...etc will be redirected to / including index.php
RewriteCond %{REQUEST_URI} ^/index.php$
RewriteRule (.*) / [R=301,L]
########################
RewriteCond %{REQUEST_URI} ^/index.php/(.*)
RewriteRule (.*) /error404.html [R=301]

I made test environment in my lan. I also created the html file with <a href="http://192.168.1.11/%20">test page</a> and restarted the server. And also clear the cache on browser, which accessing the page. On the access_log, I still got

"192.168.1.4 - - [31/Jan/2012:22:38:07 +1100] "GET /%20 HTTP/1.1" 404 276 "-" "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110621 Fedora/3.6.18-1.fc14 Firefox/3.6.18 GTB7.1" .

Now I am gonna remove the rest of the code in .htaccess and see if it works.
 
Old 01-31-2012, 06:02 AM   #6
boloo
LQ Newbie
 
Registered: Jan 2012
Posts: 15

Rep: Reputation: Disabled
When I check in error.log, I got
[client 192.168.1.4] File does not exist: /var/www/html/ . but i can go to webpage directly but typing ip address without even having index.php..
So Doc Root is set correctly in conf.
Strange...
 
Old 01-31-2012, 07:28 AM   #7
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
It's strange indeed.
Are you sure apache reads .htaccess files? Put some garbage into .htaccess and see if you get a 500 error
Also the .htaccess must be located under the docroot (/var/www/html in your case)
 
Old 01-31-2012, 02:25 PM   #8
boloo
LQ Newbie
 
Registered: Jan 2012
Posts: 15

Rep: Reputation: Disabled
I removed everything in .htaccess and wrote simple redirect a.html to b.html

RewriteRule ^a\.html$ b.html

And it didn't work. So somehow it is not reading htaccess.
 
Old 01-31-2012, 02:32 PM   #9
boloo
LQ Newbie
 
Registered: Jan 2012
Posts: 15

Rep: Reputation: Disabled
Hi Bathory,

It worked, I just put the code you suggested in production server and worked straightaway. Sorry for all the fuss, but some reason my test server is still not working.

Thank you so much again
 
Old 01-31-2012, 03:14 PM   #10
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Quote:
Originally Posted by boloo View Post
I removed everything in .htaccess and wrote simple redirect a.html to b.html

RewriteRule ^a\.html$ b.html

And it didn't work. So somehow it is not reading htaccess.
If your test box doesn't read .htaccess files, you must make sure that you have:
Code:
AllowOverride All
inside the <Directory /var/www/html>...</Directory> stanza in httpd.conf

Quote:
It worked, I just put the code you suggested in production server and worked straightaway. Sorry for all the fuss, but some reason my test server is still not working.
If you feel that your question is answered, you can mark the thread as "Solved" from the "Thread Tools" on top of the page

Regards
 
Old 01-31-2012, 04:04 PM   #11
boloo
LQ Newbie
 
Registered: Jan 2012
Posts: 15

Rep: Reputation: Disabled
Hi Bathory

Thanks again for your help. Unfortunately because of my stupid behavior, the other user name was banned. so I couldn't put solved in topic. But your help was most greatly appreciated and saved me a lot of hours.

Cheers
 
Old 07-27-2020, 03:50 AM   #12
getmeup
LQ Newbie
 
Registered: Jul 2020
Location: Delhi
Posts: 1

Rep: Reputation: Disabled
Help Me Rewrite URL

I am facing a problem rewriting an url.

Please find details below:-

Current Url: details.php?product=Newspaper%20Ad%20Agency
Need Url: Newspaper-Ad-Agency


Current Url: category-page.php?category=Newspaper%20Ad
Need Url: Newspaper-Ad-By-City
 
Old 07-27-2020, 05:38 AM   #13
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Quote:
Originally Posted by getmeup View Post
I am facing a problem rewriting an url.

Please find details below:-

Current Url: details.php?product=Newspaper%20Ad%20Agency
Need Url: Newspaper-Ad-Agency


Current Url: category-page.php?category=Newspaper%20Ad
Need Url: Newspaper-Ad-By-City
You should start a new thread posting your question giving more details, like what you're trying to do,the code you're using for this and logs if any.

And please don't hijack old threads.

Regards
 
2 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Rewriting a URL with mod_rewrite PlymWS Linux - Server 9 12-17-2011 05:42 PM
mod_rewrite remove subdirectories from url xjx424 Programming 4 04-25-2011 04:46 PM
Apache 1.3 mod_rewrite - how to rewrite url with port dlugasx Linux - Server 1 05-19-2009 10:34 AM
.htaccess - how do I strip characters from a dynamic url? using Mod_rewrite kilaz Linux - Server 3 10-15-2008 12:28 PM
mod_rewrite url redirection sqn Linux - Software 2 10-04-2006 08:39 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 04:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration