Solved

Replace space in file name - htaccess

Posted on 2011-02-25
7
942 Views
Last Modified: 2012-05-11
Hi,

I have urls like this on my website http://domainname.com/library/author/firstname lastname/ (some browsers show http://domainname.com/library/author/firstname%20lastname/) and I would like to always change the space in url to dash, so it become like this http://domainname.com/library/author/firstname-lastname/

How is this possible in htaccess, I assume is where I would do it.

Cheers,
0
Comment
Question by:rogaland
  • 3
  • 2
  • 2
7 Comments
 
LVL 2

Expert Comment

by:Aaron @KRONiS
ID: 34984947
 First to clarify...

Are you saying that you want

http://domain.com/library/author/firstname%20lastname/   to be

http://domain.com/library/author/firstname-lastname/

if so, then if you are using WordPress (are you) you can set up the permalink structure to be different to accomplish keywords in the URL.
(.e.g. permalink settings;   /%category%/%postname%/ then in the posts, edit the slugs to be keyword-phrase)

if you are asking about how to create a mod rewrite rule to change the URLs.. (not sure what you are asking) then that's different.

Usually you just want to save the new pages with a URL slug that is firstname-lastname.

I don't know what you are using... if you have wordpress.. then you can easily do this.

if not. let me know the architecture before i start answering the wrong question please.

It sounds like you want a quick mass solution, but it may be a manual solution where you just have to change all the filenames of the pages...depending on what you are using.

hope this was a helpful start.
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34985194
This .htaccess file should replace all space characters in your URL with dashes.  The [N] makes it keep going until there are no more dashes.

Options +FollowSymLinks
 
RewriteEngine On
RewriteBase /

RewriteRule ^([^\s]*)\s(.*) $1-$2 [N]

Open in new window

0
 
LVL 2

Expert Comment

by:Aaron @KRONiS
ID: 34985555
thanks community!
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:rogaland
ID: 34986334
Thanks for the fast reply, im a totally beginner in htaccess and I'm using Expression Engine and a tag module that produces tags with spaces and not dash between when there is two words.

@sjklein42; for the moment I need to use this code to remove the index.php from my url on expression engine:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(css=.*)$ [NC]
RewriteRule ^(.*)$ /index.php?/%1 [L]
RewriteCond $1 !^(images|admin|scripts|css|themes|favicon/.ico|robots\.txt|index.php) [NC]
RewriteRule ^(.*) /index.php?/$1 [L]

Open in new window


where should I put this code you have given me, before or after the removing the index.php code?

Thanks again for your help
0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 125 total points
ID: 34986740
I think this is how to combine the two .htaccess files

Options +FollowSymLinks
 
RewriteEngine On
RewriteBase /

RewriteRule ^([^\s]*)\s(.*) $1-$2 [N,L]

RewriteCond %{QUERY_STRING} ^(css=.*)$ [NC]
RewriteRule ^(.*)$ /index.php?/%1 [L]
RewriteCond $1 !^(images|admin|scripts|css|themes|favicon/.ico|robots\.txt|index.php) [NC]
RewriteRule ^(.*) /index.php?/$1 [L]

Open in new window

0
 

Author Comment

by:rogaland
ID: 34986750
Absolutely brilliant!!! Thanks sjklein42
0
 

Author Closing Comment

by:rogaland
ID: 34986751
Very happy!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my time as an SEO for the last 2 years and in the questions I have assisted with on here I have always seen the need to redirect from non-www urls to their www versions. For instance redirecting http://domain.com (http://domain.com) to http…
It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question