htaccess RewriteRule for unknown folder name not working - .htaccess

I have been working on this for a few days and had to face the facts...I'm stuck! I have a very simple RewriteRule that works well:
RewriteRule ^(.*)testfolder/(.*).php(.*)$ $1newfolder/subfolder/$2.php$3 [L]
This changes urls from something like:
http://mydomain.com/testfolder/phpfile.php#/login
to
http://mydomain.com/newfolder/subfolder/phpfile.php#/login
The problem is that I need to create a rule that will allow "testfolder" to have a dynamic name. (I have 50+ rules doing this for different folders and I need one rule that catches all of them.)
With no success, I have been trying to use the following to reproduce the results above:
RewriteCond %{REQUEST_URI} ^/[a-z]*/(.*).php(.*)$ [NC]
RewriteCond %{REQUEST_URI} !^/newfolder/subfolder/$1.php$2$
RewriteRule ^[a-z]*/(.*).php(.*)$ newfolder/subfolder/$1.php$2 [QSA,NC,L]
I need to resolve this quickly so any ideas? assistance? or help would be greatly appreciated. Thanks in advance!

Try this rule:
RewriteCond %{REQUEST_URI} !^/newfolder/subfolder/ [NC]
RewriteRule ^[^/]+/([^.]+)\.php(.*)$ /newfolder/subfolder/$1.php$2 [NC,L]

Related

Remove last part of dynamic URL with htaccess

Can any one please help with this :
i am trying to redirect this :
https://www.example.com/site/members/user-id/page/page-id/?user_filter=points
to this
https://www.example.com/site/members/user-id/
please note that /user-id/ & /page-d/ are numbers that changes in every link.
I tried searching online and tried to come up with some thing but yet no success
Here what i tried so far
RewriteRule ^site/members/[0-9]{4}/page/$ https://www.example.com/site/members/[0-9]{4}/? [R=301,NE,NC,L]
and this approach:
RewriteCond %{REQUEST_URI} ^site/members/([0-9]{4}+)$/pages/ [NC,OR]
RewriteRule (.*) /site/members/1$ [L,R=301]
Would Appreciate Any Help On that
Thanks
You can use the following rule:
RewriteCond %{REQUEST_URI} ^/site/members/([0-9]+)/pages/ [NC]
RewriteRule (.*) /site/members/%1? [L,R=301]
Make sure to clear your browser cache before testing this.

Multipule Query Strings .htaccess Redirect

Im a bit stuck where I need to redirect 80+ URLs' which have
content.php?111-rest-of-url
RewriteCond %{QUERY_STRING} ^?64-random-url-here-again$
RewriteRule ^content.php$ https://www.newdomain.com/random-url-here-again/? [R=301,L,QSD]
RewriteCond %{QUERY_STRING} ^?61-random-url-here$
RewriteRule ^content.php$ https://www.newdomain.com/random-url-here/? [R=301,L,QSD]
And there are around another 80 url's written out like this. However, when testing on site, no matter what URL you try...it always returns the first one in the .htaccess
From what I have been reading am I correct in assuming you can only use one query string of this kind ? if so is there away around this ?
Thanks in advance.
Ok so I managed to accomplish what I need by doing the following, hopefully it will help someone else.
RewriteCond %{REQUEST_URI} ^/content\.php$
RewriteCond %{QUERY_STRING} ^196-random-url-here-again$
RewriteRule ^(.*)$ https://www.newdomain.com/random-url-here-again/? [R=301,L,QSD]
RewriteCond %{REQUEST_URI} ^/content\.php$
RewriteCond %{QUERY_STRING} ^196-random-url-here$
RewriteRule ^(.*)$ https://www.newdomain.com/random-url-here/? [R=301,L,QSD]
Not sure what the difference is but it works :), would be grateful to hear from someone to explain the difference in what I have done so I understand it in the future.

htaccess redirect to certain page depending on condition

I have the following urls:
www.mydomain.com/contacto.html
www.mydomain.com/pedidos.html
www.mydomain.com/quienes.html
I want to redirect them to:
www.mydomain.com/
I have wrote the following script, but it is not working ok.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(contacto\.html|pedidos\.html|quienes\.html)$
RewriteRule ^ / [R=301,L]
Do you have any idea how to make this thing working?
Thanks in advance,
Emanuel
This is not the first time I see the problem here. I do not know why.
But I know it works like that:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(contacto\.html|pedidos\.html|quienes\.html)$
RewriteRule ^ http://www.domain.com/ [R=301,L]
Delete your cache or try from other browser if it does not work the first time.
Thanks Croises,
I've used something very similar to what you has suggested.
I did the following:
RewriteCond %{REQUEST_URI} ^/(home|pedidos|quienes|contacto).html$
RewriteRule .* / [R=301,L]
Thank you very much for your help.
BR

mod_rewrite causes 500 server error/Fails/works partially

I am trying to do a permanent redirect from my old design (www.your-translations.com) to my new design at www.your-translations.com/_YT.
In the process, the urls have to change from this format:
www.your-translations.com/my_page.php
to this format:
www.your-translations.com/_YT/index.php?pge=my_page
I have made many attempts so far, the latest of which is:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^.*your\-translations.com.*$ [NC]
RewriteRule ^(.*?)/([^/]*)\.php $1/_YT/index.php?pge=$2 [R=302]
RewriteCond %{HTTP_HOST} ^your-translations.com [NC]
RewriteRule ^(.*)$ http://www.your-translations.com/$1 [R=302,L]
This is one of my better tries in that it apparently does almost nothing (previous tries have resulted in 505 errors and infinite loops). The rewriting of URLs to "www." works properly.
I have tested the regex in different regex testers and it seems to do what I expect it to do.
Can someone explain what I am doing wrong?
WHAT exactly is the pattern applied to? Is it what is matched by the RewriteCond? The URI as I typed it in? The relative path? The actual path on the server?
Is there any way to display the string before I try to match it?
Several suggestions found in tutorials result in infinite loops on my server. Do all versions work the same way?
Unfortunately, the webhost technical support doesn't seem to know anything about mod_rewrite, so I can't expect any help from them.
According to my FTP log, I have already made about 80 attempts and I could really use a hand here.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^your-translations\.com$
RewriteCond %{REQUEST_URI} !^/_YT/([a-z0-9-_]+)\.php$
RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)\.php$
RewriteRule ^(.*) /_YT/index.php?pge=%1 [QSA]
RewriteCond %{HTTP_HOST} ^www\.your-translations\.com$
RewriteCond %{REQUEST_URI} !^/_YT/([a-z0-9-_]+)\.php$
RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)\.php$
RewriteRule ^(.*) /_YT/index.php?pge=%1 [QSA]
In RewriteCond, you must type a backslash before a dot and not before dash, because a dot in .htaccess means any character, so you need bslash to call is as dot. Sorry, I'm in mobile, so I couldn't explain more, but feel free to ask what you want to know about the code.
Thanks to some explanations from webmasterworld, I was able to fix my mod_rewrite code:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^your-translations.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.your-translations.com [NC]
RewriteCond %{REQUEST_URI} !="^.*?/.*"
RewriteRule ^([^/]*)\.php$ http://www.your-translations.com/_YT/index.php?pge=$1 [R=302,L]
RewriteRule ^$ http://www.your-translations.com/_YT/ [R=302,L]
This seems to work correctly so far (touching wood). Thanks Servant for your help.

Need help figuring out .htaccess rules

I'm pretty new to apache, and I need some help with some .htaccess rewrite rules. Here're the current rules:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/beta/index\.php$
RewriteRule ^(.*)$ /beta/index\.php [R=302,L]
Basically it redirects any requests to mydomain.com\beta\index.php, except if a filename's specified. This works fine for now.
Here's the problem:
I have a subdomain beta.mydomain.com which I don't want these rules to apply to. Any URL on beta.subdomain.com should be treated normally. However, since the .htaccess is at the server root, it's rules are messing with the beta.subdomain.com as well.
I've tried many different combinations of regex, but I can't figure it out. Help!
You need use something like this (to check if your request was related to specific domain/subdomain):
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com(:80)?$
20 seconds were missed... But my answer is better ;)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/beta/index\.php$
RewriteCond %{HTTP_HOST} !^beta\.subdomain\.com
RewriteRule ^(.*)$ /beta/index\.php [R=302,L]
This ought to do the trick!

Resources