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.
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:
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)
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),
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
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.
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?
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:
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}).
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:
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
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.
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>
RewriteRule ^index\.php$ - [L]
This is most useless rule I ever saw, please remove it
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
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:
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.