iSenne
iSenne

Reputation: 2715

Rewrite URL and remove part with .htaccess

I am really stuck with my .htaccess file, and need some help :). I have a WordPress installation that I am using for testing. It is in a folder and I use .htaccess to get there. This is the rules I use so far:

######### Custom #########
RewriteEngine On

# ignore folders
RewriteCond %{REQUEST_URI} "/af1wp/"
RewriteRule (.*) $1 [L]

###############
# only for me #
###############
# HOME (Senne Tijdeman)
RewriteCond %{REMOTE_ADDR} ^###\.###\.###\.###$
RewriteCond %{HTTP_HOST} ^((www.)?([a-z0-9_\-]+).)?alleenf1.nl$
RewriteCond %{REQUEST_URI} !^/af1wp/$
RewriteRule ^(.*)$ /af1wp/$1 [L]

This works (with my real IP address of course), so no problem there. But now I want to rewrite exisiting URL's to a new format. The old URL is this:

http://alleenf1.nl/nieuws/QOgbb/raikkonen-alles-is-mogelijk-in-australi

The new URL should be this:

http://alleenf1.nl/raikkonen-alles-is-mogelijk-in-australi

The part I want to remove "nieuws/QOgbb/" is not always the same, so I have to use regex for that. But everything I tried did not work at all.

I thought this would be simple, but apparently not for me unfortunately. Now I have 2 questions.

  1. What is the right RewriteRule to do this?
  2. Where should I put it. In the .htaccess of the root folder, or the af1wp folder where the WordPress install is?

Tnx in advanced


To awnser the questions from poncha below:

  1. Yes, the URL's always start with to folders. Just to clarify (was not clear) the part "nieuws" is always the same, but not the second part (call it an ID).
  2. I prefer a redirect.
  3. The file /raikkonen-alles-is-mogelijk-in-australi is a post in WordPress. That WordPress installation currently resides in the folder af1wp, but will be moved to the root folder when going live.

Upvotes: 0

Views: 139

Answers (1)

poncha
poncha

Reputation: 7866

Try this:

RewriteEngine On
RewriteRule ^nieuws/([^/]+)/(.*) /af1wp/$1 [R=301,L,QSA]
  1. This will only match URLs starting with "nieuws"
  2. For now, the rewrite target is /af1wp/, change it to / when moving the wordpress.
  3. When you move wordpress, you'll need to mix in this rule inside the wordpress rules, as it already has rewrite rules of its own - place this rule above its rules.
  4. The flags used here:
    • R=301 - redirect with HTTP status 301 (Moved Permanently).
    • L - last rule (stop rules parsing after successful match of this rule)
    • QSA - query-string-append (append original query string to the rewritten request).

Upvotes: 1

Related Questions