Url rewrite (strip /v2 and .php)

Hi there,

I need some help with url rewriting, here’s an example of an url:

mysite.com/v2/help/getting_started.php

what I would like to do is to rewrite this to

mysite.com/help/getting_started

and it should ofcourse work with all files,

is there someone who can help me setting that up in the .htaccess file?

Thanks a lot! :slight_smile:

az,

The last code supplied says: If prepending v2. to the request finds either a file OR a directory, redirect to the v2/ version. Then, if the file or directory does not exist, redirect to your CMS handler, index.php. In other words, it should be doing EXACTLY what you’ve asked for.

Because I haven’t tested the code locally, there may be a problem with //'s or lack of /'s in the first bit of code (the v2/ group) but I’ve left that testing to you. Use the R=301 flag to see whether the v2 redirections work then remove once you have the correct set of /'s.

Regards,

DK

Wordpress have a way to make the URLs seo friendly (don’t ask me how they do it) for example www.zorps.dk/funktioner (instead of www.zorps.dk/?p=ID) that now gives me a 404, and trying to access any other files in the v2 folder without v2 in the URL doesn’t work either i’m afraid (also a 404).

I’m afraid I cannot leave it on the server with the wordpress, I have however made another v2 directory inside a rewrite directory with a file: test.html and inside the rewrite_test directory I have the .htaccess file:

www.zorps.dk/rewrite_test/v2/test.html

so I should be able to access that file with:

www.zorps.dk/rewrite_test/test.html

but it still shows a 404..

Thanks for helping,

None of it seems to be working, maybe wordpress’es rewrite rules are interfearing?

here’s what I’ve got:


# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
RewriteCond v2/%{REQUEST_URI} -d
RewriteRule .? v2/%{REQUEST_URI} [L]

What do you mean by that? What do you see when you go to a normal wordpress page?
And my code indeed doesn’t work. As dklynn correctly pointed out I didn’t take files into account (just directories) :blush:

Thanks for helping both of you.

However, both things doesn’t work,


RewriteCond %{REQUEST_URI} !v2/
RewriteCond %{DOCUMENT_ROOT}/v2%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}/v2%{REQUEST_URI} -f
RewriteCond .? v2%{REQUEST_URI} [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

This code breaks wordpresses friendly url’s, and still doesn’t work with the other files (eg trying to go to mysite.com/login.php (which is located mysite.com/v2/login.php) doesn’t work),

@ScallioXTX

Your code didn’t work either.

To make these rewrites you need to use Apache’s mod_rewrite
A very good article to get you started with mod_rewrite can be found here: http://www.datakoncepts.com/seo

Take a look at that and feel free to come back here if anything isn’t clear :slight_smile:

Hi,

Thanks.

I’ve managed to hide my .php extention however I can’t get a way to make:

mysite.com/v2/help/getting_started

into

mysite.com/help/getting_started

I first tried just to make it into

mysite.com/v3/help/getting_started

and this is where I’m stuck, i’ve used the following code:
RewriteRule ^/?v2/([a-z/.]+)$ v3/$1 [R=301,L]

this just gives me a site cannot be found.

Any help?

Ah, now we’re getting somewhere :slight_smile:

Okay, so you already had this rule:


RewriteRule ^/?v2/([a-z/.]+)$ v3/$1 [R=301,L]

Since you want the users type one URL and keep the URL in the address bar in the browser but actually see another URL you don’t want to use an external redirect, so ditch the R=301

Next, the rule above redirects v2/whatever to v3/whatever, but what you want is to redirect from /whatever to /v2/whatever.

So, something like this:


RewriteRule ^/?([a-zA-Z0-9_.-]+)$ v2/$1 [R=301,L]

Note that that rule will rewrite everything from / to /v2, so if there are also images, css files, js files etc that need not to be rewritten to v2, make sure they don’t get redirected by adjusting the regular expression in the RewriteRule

Alternatively, you could also use this


RewriteEngine On
RewriteCond v2/%{REQUEST_URI} -d
RewriteRule .? v2/%{REQUEST_URI} [L]

That will redirect any request to a file that also exists in the v2 directory to the v2 directory. I didn’t test the code but it should work.

Sorry if I’m not clear

I have a file: mysite.com/v2/login.php

but I want the user to be able to access that file (mysite.com/v2/login.php) by going to

mysite.com/login.php

If v3 doesn’t exist than of course it’ll show you a file not found.
Question is, does it show the correct URL?

By the way, ([a-z/.]+) matches lower case characters, slashes and dots.
I’d say ([a-zA-Z0-9_-]+) is more appropriate for your problem.

Okay, that’s right.

Is there no way to use the new rewrite rule for stripping v2 from the url only if a file within the v2 folder is found (eg if user types mysite.com/fileinv2directory), if the file is not found then use the wordpress rule?

Rémon,

EXCELLENT tutorial - until you got to the redirection requested. How did

RewriteRule ^/?v2/([a-z/.]+)$ v3/$1 [R=301,L]

get replaced by

RewriteCond v2/&#37;{REQUEST_URI} -d
RewriteRule .? v2/%{REQUEST_URI} [L]

That’s “prepend the request with v2/, test whether that’s a directory then redirect ANYYTHING to v2/request”!

Okay, the OP was confused about the direction of the redirection, i.e., he wants to REMOVE v2/ from the VISIBLE path - so the redirection must omit v2/ in the regex and add it to the redirection WITHOUT the R=301. Also, IMHO, both the file and directory need to be checked before redirecting to v2/{REQUEST_URI} so that becomes:

RewriteCond %{REQUEST_URI} !v2/
RewriteCond %{DOCUMENT_ROOT}/v2%{REQUEST_URI} -d [COLOR="Red"][OR][/COLOR]
RewriteCond %{DOCUMENT_ROOT}/v2%{REQUEST_URI} -f
Rewrite[COLOR="Red"]Rule[/COLOR] .? v2%{REQUEST_URI} [L]

Two things of note:

  1. The file and directory checks REQUIRE the physical path to the file/directory so either use {REQUEST_FILENAME} or some kludge like %{DOCUMENT_ROOT}%{REQUEST_URI} (e.g., %DOCUMENT_ROOT}/v2%{REQUEST_URI}).

  2. Because the {REQUEST_URI} variable begins with /, the / must be omitted after %{DOCUMENT_ROOT}/v2.

That’s a sticky one but you got an excellent start on it! :tup:

Regards,

DK

First you say you want to rewrite /v2/anything to /anything, and now you’re saying you want rewrite /anything to /v2/anything
Which is it?

that still gives me a 404…

here’s the files i got (example):


webroot
   -index.php
   -blabla.php
   v2
      -login.php
      -blabla.php

So lets say, I want a user to access the file login.php which is in the v2 folder, but I don’t want the v2 folder to show up in the URL so instead of going to

mysite.com/v2/login.php

the user should be able to go to

mysite.com/login.php

still without showing the real url (mysite.com/v2/login.php) to the user.

Sure there is, and you almost had it as well:

Just remove v3/ from that rule.
That will redirect

/v2/whatever/directory/or/file/the/visitor/wishes/to/see

to

/whatever/directory/or/file/the/visitor/wishes/to/see

That is what you want right, or am I missing something?

azarat,

Of course it doesn’t work when you have WP redirecting to capture EVERYTHING which is also not a file (or directory). One or the other but you need to pick one - OR some way to distinguish between the two. For example, if your v2 files are .html files, then change the first RewriteRule (whoops - my error in the original) to require the .html extension.


RewriteCond %{REQUEST_URI} !v2/
RewriteCond %{DOCUMENT_ROOT}/v2%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}/v2%{REQUEST_URI} -f
Rewrite[COLOR="Red"]Rule[/COLOR] .? v2%{REQUEST_URI} [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Regards,

DK

Yikes! That .htaccess really scares me!

  1. If you know you’re server supports mod_rewrite, don’t use <IfModule>, that’ll make Apache check whether mod_rewrite is enabled on each request. Since you already know it is, you’re wasting CPU cycles for nothing. Please get rid of the <IfModule></IfModule>

  2. RewriteRule ^index\.php$ - [L]
    This is most useless rule I ever saw, please remove it

  3. RewriteBase is only used to reset any path changes made by mod_alias, since you don’t use mod_alias, please remove that line as well

Good, so now you have:


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

RewriteCond v2/%{REQUEST_URI} -d
RewriteRule .? v2/%{REQUEST_URI} [L]

And yes, the Wordpress rules are specified before the rewrite from / to /v2 so they will be processed first and the redirect will never take place. You need to swap them around:


RewriteEngine On
RewriteCond v2/%{REQUEST_URI} -d
RewriteRule .? v2/%{REQUEST_URI} [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

well the thing is that I have a folder v2, and I kind of need to have a sub folder (for different reasons) but I don’t really want my users to see that.

So is there no way to make a link such as:

mysite.com/v2/help/getting_started

into

mysite.com/help/getting_started

without moving the files?

Hi,

It works now, thanks!

There’s only one problem, when I enter mysite.com it automaticly redirects to the v2 folder, how do I fix that?