I have a multy language site serverd by Apache2 webserver. Structure is
site.com - main version
site.com/fr - for france
site.com/it - another lang
I have a problem when i am trying to get static resources which are located in root direcory, like
site.com/js/bundle.js - right
site.com/fr/js/bundle.js - wrong, because CMS sets base URL as site.com/fr
How can i strip language part from URL with .htaccess for static files img, js etc.
All languages are known.
Try with below,
RewriteEngine On
RewriteCond %{REQUEST_URI} ([\w]+)/([\w]+).\(js|css|vbs)$
RewriteRule ^ %1/%2.%3 [R,L]
Related
I have a web server with the following (simplified) layout:
/
www/ # will hold html and PHP files (web content)
doc/ # some documentation
lib/ # some libraries
I would like to use htaccess to "redirect" every request to www.mydomain.com/page to www.mydomain.com/www/page.php.
I have tried the following:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^(.*)$ www/$1.php
But it produces a 500 Internal Server Error.
For debugging purposes, I created a www/test.php page which echoes every GET variable, and I modified my .htaccess:
RewriteRule ^(.*)$ www/test.php?page=$1
just to check whether I'm matching the right things.
The expected behaviour when performing a request against www.mydomain.com/somepage would be page=somepage, instead I get page=www/get.php.
Why this behaviour? How can I accomplish what I need?
You have to exclude the path you are rewriting to :
RewriteRule ^((?!www).*)$ www/test.php?page=$1 [NC,L]
otherwise you will get an infinite loop error because www/test.php also matches the rewrite pattern (.*)
I have the following URL:
http://example.com/media/image.png
This should be redirected to:
http://example.com/media/images/image.png
This rule is only to redirect images in media folder.
But the rule should NOT redirect this kind of URLs:
http://example.com/some_folder/media/image.png
To:
http://example.com/media/images/image.png
How it should be?
I tried things like this:
RewriteRule /media/(.*) /media/images/$1 [R,L]
But I didn't get good results.
You need to anchor your expression, otherwise it will match any URL containing /media/ as opposed to starting with media/
RewriteRule ^media/(.*) /media/images/$1 [R,L]
If it's only images, you might want to consider making the expression more strict, such as:
RewriteRule ^media/(.+\.(jpe?g|gif|png)) /media/images/$1 [R,L]
I am creating mobile site for an existing site using "m" subdomain.
I am stuck with htaccess rules. I am very new to htaccess and very bad with regex.
Site Name - xyz.com
Subdomain - m.xyz.com
I want that whenever my webpage is redirected( OR directly opened in) to mobile, subdomain should
have exact same URL as main domain but should go to a mobile version file i.e.
Player
domain - xyz.com/5 -- real url - xyz.com/account.php?ln=5 (this account.php is in root folder)
Subdomain - m.xyz.com/5 -- real url - m.xyz.com/account.php?ln=5 (this account.php is in subfolder m)
Team
domain - xyz.com/TeamName/10 -- real url - xyz.com/team.php?ln=10 (this team.php is in root folder)
Subdomain - m.xyz.com/TeamName/10 -- real url - m.xyz.com/team.php?ln=10 (this team.php is in subfolder m)
Game
domain - xyz.com/GameName/18 -- real url - xyz.com/game.php?p=18&t=game (this game.php is in root folder)
Subdomain - m.xyz.com/GameName/18 -- real url - m.xyz.com/game.php?p=18&t=game (this game.php is in subfolder m)
xyz.com works perfectly for all combinations but when I hit subdomain it does not point to correct file.
when i try to hit m.xyz.com/TeamName/10 it redirects to http://m.xyz.com/m/game.php/?p=45&t=TeamName but should have gone to
m.xyz.com/team.php?ln=5 (to the file in subfolder m). Direct URL works.
Directory Structure :
xyz(htdocs)
|
--- account.php
|
--- team.php
|
--- game.php
|
--- m (Subdirectory - Subdomain)
|
--- account.php
|
--- team.php
|
--- game.php
.htaccess file -
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !([a-zA-Z0-9]\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
#Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
#player 1 Parameter = Account Id
RewriteRule ^([0-9]+)\/?$ account.php?ln=$1 [NC]
#team 2 parameter = team name + team ID
RewriteRule ^([a-z0-9_-]+)\/([0-9]+)\/?$ team.php?ln=$2 [NC]
#game page = game ID + game Name + type
RewriteRule ^([a-z0-9_-]+)\/([a-z0-9_-]+)\/([0-9]+)\/?$ game.php?p=$3&t=$2 [NC]
# End of Apache Rewrite Rules
</IfModule>
Please have a look and let me know if you have any answer, searched on google for almost three days already no luck yet.
Ahh Nailed it.. it was a very simple catch .. dint realize i was missing the .htaccess in my subdomain folder (root/m). Copied the same htaccess in subdomain and it worked like charm. in simple words for other people who may try to find the answer for it...
Domain and subdomain both require a .htaccess file, if you are trying to create an exact domain copy site for mobile site you must be having files with same name in your subdomain directory you need to just place a copy of htaccess file in your subdomain which you are using for domain.
Dint realize it was so easy... stupid me... (This is how you learn :D )
I have reviewed the many questions posted here related to .htaccess, apache, mod-rewrite and regex, but I'm just not getting it. I tried a few different things but either I am over complicating things or making beginner mistakes. Regardless, I've been at it a few days now and have completely scrambled things somewhere as the 10000 404's per day are showing.
My site
I have a WordPress site which contains over 23,000 posts broken down into just over 1200 categories. The site features streaming video files, industry news, show reviews, movies, phpbb forums, etc. and is structured like this:
site / base categories ( 0 and a-z) / sub categories (series name) /
posts (episode name .html )for all streaming media episodes
site / movies / post title.html for all streaming movies
site / news / posttitle.html
site / reviews / posttitle.html
site / page.html for assorted pages
site / forums
Permalink structure is /%category%/%postname%.html
I have am using the Yoast Wordpress SEO plugin and have the option to append a trailing slash enabled for directories and categories.
here is the current .htaccess
# 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
My examples
From our old site structure we have many inbound links using "/episode title/". This is wrong. We need these incoming links to redirect to /watch-anime/letter, number or symbol only 1 character long/series title/episode title.html
/one-piece-episode-528/
should be
/watch-anime/o/one-piece/one-piece-episode-528.html
A mistake I made caused this problem... "/watch-anime/letter/series title/episode title/" to "/watch-anime/letter/series title/episode title.html". So, we need to remove trailing slash from single posts and add .html
/watch-anime/w/welcome-to-the-nhk/welcome-to-the-nhk-episode-14/
should be
/watch-anime/w/welcome-to-the-nhk/welcome-to-the-nhk-episode-14.html
The same mistake caused this problem when combined with the old site structure issue... "/episode title.html" needs to be "/watch-anime/letter/series title/episode title.html"
/one-piece-episode-528.html
needs to be
/watch-anime/o/one-piece/one-piece-episode-528.html
As you can see, I've made a mess of things between migrating the sites post structure and my attempts to fix it. I am now asking for any help you can provide in getting a proper .htaccess file that will take care of these 301 redirects.
Thanks for any assistance you can provide!
I don't know if RewriteMap work with .htaccess files, but anyway here's my solution for virtual host, which should work flawlessly.
Create a RewriteMap file. See here for more information. This is a very simple text file with: first, the wrong URL without the '/', then one space (at least) and then the right url, like this:
one-piece-episode-528 /watch-anime/o/one-piece/one-piece-episode-528.html
dexter-season-6-episode-1 /watch-interesting-stuff/d/dexter/dexter-season-6-episode-1.html
breaking-bad-full-season-3 /watch-interesting-stuff/b/breaking-bad/breaking-bad-full-season-3.html
and so on.
convert this simple text file into hash map. For example:
httxt2dbm -i mapanime.txt -o mapanime.map
Now declare it in your vhost:
RewriteMap mapanime \
dbm:/pathtofile/mapanime.map
So all in all your vhost should look like:
<VirtualHost *>
RewriteEngine On
RewriteMap mapanime \
dbm:/pathtofile/mapanime.map
# don't touch the URL, but try to search if it exists in mapanime
RewriteRule /([^/]*)/$ - [QSA,NC,E=VARANIME:${mapanime:$1|notfound}]
# if VARANIME not empty *and*
# VARANIME different from "notfound":
RewriteCond %{ENV:VARANIME} ^(notfound|)$
# then redirect it to the right URL:
# QSA = query string append
# R = redirect, 301 = definitive redirect
# L = last = don't go further
RewriteRule . %{ENV:VARANIME} [QSA,R=301,L]
</VirtualHost>
Hope this helps.
I don't see a simpler solution, but I'm pretty sure this one will work.
If it doesn't work: read my usual "two hints", and add the rewrite log in your question.
Two hints:
Please try to use the RewriteLog directive: it helps you to track down such problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)
I'm trying to send page requested through a front controller.
I need to match the following urls
domain.com/admin/
domain.com/admin/somepage
but I also have some assets in the /admin subfolder that I DONT want to match
domain.com/assets
domain.com/assets/mything.css
domain.com/assets/mything.css
domain.com/xml/myxml.xml
I have written the following rule (inside .htaccess in the root of the site and that works for all 5 example URLs. How do I get it to match the top two ONLY?)
RewriteBase /
# index.php is the front controller
RewriteRule ^(admin) admin/index.php [L,QSA]
There are two ways I can see of doing this (I'd like to know how to do it using the folder 'assets' and 'xml' as an exclusion AND by setting extensions)
I'd use a negative RewriteCond before the final routing rule along these lines:
RewriteCond %{REQUEST_URI} !assets