Writing htaccess that reroutes to other folder - apache

I'm trying to write a rule that redirects a request to a different file/path. Here is a sample of the incoming URL: /js/library/sample.js, has to reroute to /private/package/library/js/sample.js.
I tried:
ReWriteCond ^/js/(A-Za-z_]+)$/(A-Za-z.]+)$ /private/package/$1/js/$2
I'm finding it difficult to write the rule correctly though.
Thanks.
PS. Any good htaccess/rejex tutorials, send them along.

Do this:
RewriteRule ^(js)/([\w]+)/([\w.]+)/?$ /private/package/$2/$1/$3 [NC,L]
I found these Apache docs sufficient to learn.
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
http://httpd.apache.org/docs/2.0/rewrite/

Related

Apache Mod_Rewrite Question Mark

I need to redirect an incoming request with the following URL:
http://mywebsite.com/abc/mapserv.exe?map=123
to
http://mywebsite.com/abc/mapserv.exe?map=C:\Mapserver\ms4w\Apache\htdocs\Mapfiles\123.map
I already managed to do simple mod_rewrites but the question mark is killing this one all the time. I am not able to adapt common Query String examples to my case so I need help with this exact case.
As though you did not show your try, you could test this:
RewriteEngine On
RewriteCond %{QUERY_STRING} map=([0-9]+)$
RewriteRule . %{REQUEST_URI}?map=C:\\Mapserver\\ms4w\\Apache\\htdocs\\Mapfiles\\%1.map [NE,L]
Rewrite flags used:
NE: Not Escape,
L: Last instruction to run.
I was still having trouble with the .exe url since it is not accessible if you dont deliver the parameters right when you send the request. And then the redirect wont fire. So I made a dummy mapserver.php file which allows setting a parameter like so:
http://mywebsite.com/abc/mapserver.php?map=123
After hours of trying I ended up with the following RewriteRule:
RewriteCond %{QUERY_STRING} ^map=(.*)$
RewriteRule ^mapserver.php?$ /cgi-bin/mapserv.exe?map=C://Mapserver//ms4w//Apache//htdocs//Mapfiles//%1.map

Apache - redirect only the domain URL to a subpage

I have a shared hosting Apache server, and I'm trying to send visitors who come to the main domain URL to a specific page, with the URL replaced and a 303 redirect:
example.com
example.com/
to
example.com/subdirectory/page.html
Only the plain domain URL should get redirected, not:
example.com/page.html
example.com/otherdirectory
example.com/otherdirectory/
example.com/subdirectory
example.com/subdirectory/
example.com/subdirectory/otherpage.html
example.com/subdirectory/otherdirectory
example.com/subdirectory/otherdirectory/
example.com/subdirectory/otherdirectory/page.html
I'm not sure that RewriteEngine is allowed on this server, if there are alternative approaches possible.
The crude way I've thought of is to just use DirectoryIndex to send the visitor to example.com/index.php -- and have a PHP redirect in that file go where I want. But I'm not sure if this might produce a visible blip for some visitors, or how Google would feel about it.
I've found other instances of this kind of question on Stack Overflow, but the answers are failing for me in some way or another. As the behavior is not intuitive, testing before posting might be advisable.
Thanks
If mod_rewrite is not enabled you can use mod_alias based rule like this in your DocumentRoot/.htaccess:
RedirectMatch 303 ^/?$ /subdirectory/page.html
UPDATE: Equivalent mod_rewrite rule:
RewriteEngine On
RewriteRule ^/?$ /subdirectory/page.html [L,R=303]

Apache and Japanase website re-write module

I use to work on a multilingual website, within the next few months, we added Korean and we are now adding Japanese. Apart the fact that it generate a huge list of 404 errors on the logs files, I am having another issue with the urls. The Apache server response with a Bad request when I insert this url:
学校-詳細な評価-11
#[.htaccess]
RewriteRule ^学校-詳細な評価-(.+)$ /statistics.php?n=$1 [L]
I tried also using the [B] flag, but nothing changes.
I tried as well to remove character and I found that it only works if I do in this way:
#[.htaccess]
RewriteRule ^学-(.+)$ /statistics.php?n=$1 [L]
But of course I cannot avoid the issue in this way :D
I notice that Apache will convert the url to:
%E5%AD%A6%E6%A0%A1-%E8%A9%B3%E7%B4%B0%E3%81%AA%E8%A9%95%E4%BE%A1-11
Then I tried it as well:
#[.htaccess]
RewriteRule ^%E5%AD%A6%E6%A0%A1-%E8%A9%B3%E7%B4%B0%E3%81%AA%E8%A9%95%E4%BE%A1-(.+)$ /statistics.php?n=$1 [L]
But I'm still having the issue :(
Does anyone know how to fix it or why this is happening? I did a research but all forums and website only suggest to use the flag B at the end of the url.
Any help is welcome! Thank you :)
Try in Hex:
RewriteRule ^\xE5\xAD\xA6\xE6\xA0\xA1-\xE8\xA9\xB3\xE7\xB4\xB0\xE3\x81\xAA\xE8\xA9\x95\xE4\‌​xBE\xA1-(.+)$ /statistics.php?n=$1 [L]
For example with having this rule:
RewriteRule ^\xE5\xAD\xA6-(.+)$ /statistics.php [L]
this url http://domain.com/学-t will redirect to http://domain.com/statistics.php
For testing you can use online htaccess tester: http://htaccess.madewithlove.be/ and remember to change the page encoding to UTF-8 before testing.

Apache Mod_Rewrite Scenario

I was wondering how I would do a complex mod_rewrite. Below is basically how I want it done.
If the user goes to:
-http://files.stuff.example.txt.r.site.com/doc.txt
Then the server would rewrite the url to:
-http://r.site.com/index.php?type=txt&username=example&dir=files.stuff&file=doc.txt
Better picture:
-http://[dir3-dir2-dir1].[username].[type].r.site.com/[file]
Rewrites to:
-http://r.site.com/index.php?type=[type]&username=[username]&dir=[dir3.dir2.dir1]&file=[file]
I created a colour coded image to clearly show what I mean:
(can't embed images) look here:
http://i.stack.imgur.com/24H8j.png
The first subdomains are a directory structure (shown in red), so the amount of subdomains can change.
I hope someone can provide me with a solution. Either using mod_rewrite or maybe another method. Thanks.
Provided that you have configured your DNS so that requested URL hits server where your application is (maybe wildcard DNS on your domain: *.site.com -> 123.45.67.89, if supported by your DNS server/hosting), you can create more or less complicated rewrite rule. I'd do it this way:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).r.site.com$
RewriteCond $1 !^index.php
RewriteRule (.*) index.php?subdomain_part=%1&file_part=$1
So in index.php you get $_GET['subdomain_part'] and $_GET['file_part'], which you can parse further to extract parameters according to your convention.
Of course, you can write more complicated regex to get URL parts extracted by mod_rewrite (I'm not such an regex expert myself). However doing parsing in PHP would be much easier and you can do better error handling (e.g. if URL is not formed properly).

Using .htaccess mod_rewrite to pass all URLs in a given directory to a single redirect script

Im trying to use mod_rewrite to redirect any call to /real-estate/* to rewrite.php...i know i can redirect everything to rewrite.php with this:
RewriteRule ^(.*)$ rewrite.php?url=$1 [L]
I would like to have my urls formatted like /real-estate/12345/123-anywhere-st ....where the 123-anywhere-st would be ignored, and have /real-estate/12345 sent to rewrite.php...id like the rewrite rule to only be used on /real-estate...all other areas of the site should function as is...Ive searched all over for a good tutorial or cheat sheet, but none that I can find actually explain how to format the mod_rewrite rules, they just give one or two examples and thats it...can anyone help, as well as maybe provide a link to somewhere I can learn
Thanks!
RewriteRule ^/real-estate/(.*)$ rewrite.php?url=$1 [L]