.htaccess and subdirectories - regex

I am trying to make a .htaccess file that is basically a wildcard setup. I have a folder structure as below.
public_html/sites is the root directory and in this folder there are two sub directories:
public_html/sites/brand (there are many brand folders, just used brand as example)
public_html/sites/brand/event (there are many events in a brand folder, just used event as example)
I have files in each event folder such as index.php and media.php
I have been unsuccessful in rewriting the media page to the URL structure I am looking for. Below is my current .htaccess file.
RewriteEngine On
RewriteRule ^media/([^/]*)$ /media.php?unqid=$1 [L]
So the expected URL I am wanting is /testbrand/4177/media/123456. I will have serveral brands and several events under each brand so I am needing some sort of wildcard if possible. Any help is appreciated. Thanks in advance.

If you are wanting to have a URL like this
http://example.com/testbrand/4177/media/123456
And your brands are dynamic, then your rewrite should probably look something like this.
RewriteRule ^(?:[^/]+)/(?:[0-9]+)/media/([0-9]+)/?$ /media.php?unqid=$1 [L]
Based on your current internal URL you'll only be sending the id number that comes after /media/ to PHP. If you have more info you need to send, then you need to update your internal URI parameters.

You can use this rule in your root .htaccess:
RewriteEngine On
RewriteRule ^([^/]+/[^/]+)/media/(\d+)/?$ $1/media.php?unqid=$2 [L,NC,QSA]
This will load /testbrand/4177/media.php?unqid=123456 when you request /testbrand/4177/media/123456

Related

htaccess redirect to domain based on string in url

We have a site where content is generated from CMS. The issue is people have added links to their website without the http:// hence all the links got generated in a wrong way.
for eg: http://www.example.com/ec/shefaliaxena/eventupdate/www.shefalisaxena.com
What I would like to do is get the data after last slash and check if it is valid domain using regex and if it is valid domain then redirect it to that domain or else do nothing.
I was able to extract the data after last slash using regex ([^/]+$) but wondering how do I acheieve this redirect.
I could do this easily with the coding but I would like to handle this from .htaccess file itself so that it doesn't add burden on the server.
A problem you might run into trying to do this is that valid filenames often look a lot like valid domain names, so you'll want to make sure something that looks like a domain name isn't a valid file before trying to forward the URL:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /(([A-Za-z0-9-]+\.)+[A-Za-z]{2,6})$ http://$1/ [L,NC,R=301]
If you're generating the URLs from data users have entered, I'd definitely look at adding the protocol there instead of doing it this way.

.htaccess redirect root to folder

I have two independend websites on my server. Of which one website is located in a subfolder of the other. Both websites are accessible over two different domains. Following the folder structure on my server:
/www <- one.com
/index.html
/assets
...
/folder <- two.com
/.htaccess
/index.html
/assets
...
Therefore, if I want to access website two I can do so with the following URLs:
one.com/folder
two.com
My problem: Using absolute paths to include files (css/js/img) in website two, I need to adapt my root depending over which URL the user access website two. Following an example for illustration:
The html file /www/folder/index.html contains an image:
<img src="/assets/img.jpg" />
If the user access this html file via the domain one.com/folder/index.html the image source path gets translated to one.com/assets/img.jpg where it won't find any image. Correct would have been one.com/folder/assets/img.jpg (one notice the folder in the path). Obviously, if the user calls two.com/index.html the problem doesn't exist.
Therefore, I thought I would change the root (base directory) of website two via .htaccess mod rewrite depending on which URL gets called. Following the simple pseudo code I would like to implement:
if url == one.com/folder*
root = /folder/
else
root = /
And following what I got so far for my .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?one\.com
RewriteRule ^/(.*)$ /folder/$1 [R]
Unfortunately, it seems as if I am missing something here. So far it doesn't work. Any advice is much appreciated!
You can place this rule in /www/.htaccess folder:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(assets/.+)$ /folder/$1 [L,NC,R=301]

Display Folder Contents External of CFWheels Influence

My host provider has CFWheels set up as the root www folder I can access. However, I am trying to add a folder that is independent of the framework. In folder foo/ I added a blank application.cfc as told here in the folder. But, when I upload the folder and try to view it on mywebsite.com/foo I get
Could not find the view page for the index action in the Admin controller.
Could the host have disabled my ability to do this? I also read that you can place files you don't want CFWheels to affect by adding them to miscellaneous/ but want to avoid that if possible.
Two things to check:
1) Have you excluded 'foo' in any URL rewriting files - i.e, .htaccess (apache etc) or web.config (IIS etc)? If you add it there it will bypass wheels easily.
i.e:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^.*/(foo|flex2gateway|jrunscripts|cfide|cfformgateway|CFFileServlet|cffileservlet|railo-context|files|images|javascripts|miscellaneous|stylesheets|robots.txt|favicon.ico|sitemap.xml|rewrite.cfm)($|/.*$) [NC]
RewriteRule ^(.*)$ ./rewrite.cfm/$1 [NS,L]
2) If you're not using url rewriting, then it might be there's a catchall route in your config/routes.cfm file.
Do you have anything with a wildcard there?
i.e
addRoute(name="catchall", pattern="*", controller="admin", action="index");
Try putting some content in your /foo/Application.cfc file.
component {
this.name = "foo";
}
Also, make sure the file name starts with a capital "A" - case matters on Unix/Linux servers.

.htaccess redirect to subdirectory

We have our company websites and in that we have careers tab . All files of our company website are placed into a root folder.So for different career openings we created different pages which are being placed inside the root folder for e.g http:/mycompany.com/androidDeveloper.php.But now we want url to look differently i.e .When androidDeveloper.php is openned in Url I want it to be seen as http:/mycompany.com/Careers/androidDeveloper.php but I don't want to create new directory as mycompany.com/careers in my root directory is there any way where in above mentioned can be possible
I tried doing this in .htaccess file but doesn't seems to work
Redirect http://mycompany.com/androidDeveloper.php http://mycompany.com/careers/androidDeveloper.php
You can try this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^careers(/.*)?$ $1 [L,NC]

Rewrite Rule to redirect for media files

We recently moved a site to a new domain and now need to redirect any requests for pdf files.
All pdfs are now stored in one folder on the new server called "oldmedia" so we need to redirect all requests for pdfs to the new folder.
Essentially, all old links look like this: http://olddomain.com/Publications/file.pdf
And they need to be redirected to http://newdomain.com/oldmedia/file.pdf
All pdf urls will be coming from the Publications sub-directory so we've been trying to check "if it starts with Publications and ends with .pdf", but I can't get it to work.
Here's what I have so far:
RewriteRule ^(/Publications/)[.+](\.pdf)$ oldmedia/$2.pdf [R=301, NC, L]
Any help would be greatly appreciated!
You probably need a rule like:
RewriteRule ^Publications/([^/]+\.pdf)$ http://newdomain.com/oldmedia/$1 [R=301,NC,L]
If the rule is in the root of olddomain.com.