Jay Tee
Jay Tee

Reputation: 13

.htaccess mod_rewrite with dynamic folders

I have been trying this for a little while nut just cannot get to work:

RewriteRule ^([^])/([^])\.php$ /$1.php [L,QSA]

I have a site: www.mysite.com/dummyfolder/file.php

I want it to actually load:

www.mysite.com/file.php

FYI - dummyfolder is dynamic and thus changes.

The more complex part, is I would like to be able to ignore this on certain folders, for example be able to access: www.mysite.com/acutalfolder/file.php. Where acutalfolder exists and does not change its name.

Thanks so much - it has really been bogging me down and just cannot figure it out.

Ansari, thank you for answering and solving, however I need to correct my actual folder statement:

My actual folder is located here:

www.mysite.com/acutalfolder/file.php 

but I want to appear in the URL like this:

www.mysite.com/dummyfolder/acutalfolder/file.php 

if i use this (to try and ignore if I am using actualfolder):

RewriteRule ^([^/]+)(!^actualfolder$)/([^/]+)\.php$ /$2.php [L,QSA] 

It works fine going to the actualfolder address, but then the original rewrite pages no longer work. Any ideas where I am hitting this wrong?

UPDATE After help from Ansari. This now works beautifully with the following code:

RewriteCond %{REQUEST_URI} ^/([^/]+)/(actualfolder/.*\.php)$
RewriteCond %{DOCUMENT_ROOT}/%2 -f
RewriteRule .* %2 [L,QSA]


RewriteCond %{REQUEST_URI} ^/(?!actualfolder)([^/]+)/(.*\.php)$
RewriteCond %{DOCUMENT_ROOT}/%2 -f
RewriteRule .* %2 [L,QSA]

Many thanks...

Upvotes: 1

Views: 928

Answers (2)

Jay Tee
Jay Tee

Reputation: 13

UPDATE After help from Ansari. This now works beautifully with the following code:

RewriteCond %{REQUEST_URI} ^/([^/]+)/(actualfolder/.*\.php)$
RewriteCond %{DOCUMENT_ROOT}/%2 -f
RewriteRule .* %2 [L,QSA]


RewriteCond %{REQUEST_URI} ^/(?!actualfolder)([^/]+)/(.*\.php)$
RewriteCond %{DOCUMENT_ROOT}/%2 -f
RewriteRule .* %2 [L,QSA]

Many thanks...

Upvotes: 0

Ansari
Ansari

Reputation: 8218

Try this:

RewriteRule ^([^/]+)/([^/]+)\.php$ /$2.php [L,QSA]

As for when actualfolder exists, you don't need to do anything for that, unless you do some rewriting in another .htaccess file within actualfolder.

EDIT after OP re-did the question:

RewriteCond %{REQUEST_URI} ^/([^/]+)/(actualfolder/.*\.php)$
RewriteCond %{DOCUMENT_ROOT}/%2 -f
RewriteRule .* %2 [L,QSA]

RewriteCond %{REQUEST_URI} ^/([^/]+)/(.*\.php)$
RewriteCond %{DOCUMENT_ROOT}/%2 -f
RewriteRule .* %2 [L,QSA]

Upvotes: 2

Related Questions