Apache rewritemod redirecting to realpath - regex

I'm working on a streaming app and I want my links to be as easylooking as possible. I choosed to use the rewritemod using the format /streaming/stream_type/stream_name/season/episode/version, exemple /streaming/ser/stargate_atlantis/2/13/vf.
So I created a .htaccess into the streaming directory with the rule:
RewriteRule ^(ser|mov)/(?:(\w[\w_]*\w)/(?:(\d+)/(?:(\d+)/(\w{0,10})?)?)?)?$ _dispatch.php?type=$1&sn=$2&s=$3&ep=$4&ver=$5 [L]
It works perfectly, but now I want the users that calls /streaming/stream_type to be redirected to /streaming/stream_type/ adding the slash for him, and the same for the series name, the season, etc. I looked on the web and saw you can add [R=301] so the browser redirects to the replaced uri and not only apache
I tried to add the following rule for try if it really works:
RewriteRule ^ser$ ser/ [L,R=301]
But I get a redirection to the realpath of the file like this: http://wawolf/streaming/ser => http://wawolf/C:/wamp/www/wawolf%202.1/streaming/ser/
What's happening? It's a simple replace, but apache redirects it to http://server/absolute_path/, not even http://server/streaming/absolute_path while .htaccess which contains the rule is inside the streaming directory.
Worst, I removed the directive, restarted apache, my computer, and it's still doing the same thing!
Thank you in advance, Pierre, that needs a Xtra large buoy.

It is due to absence of RewriteBase.
Or just have full path in target:
RewriteRule ^ser$ /ser/ [L,R=301]

Related

.htaccess: Rewrite path internally without redirecting

I can access my web server as follows: https://www.example.com/my_old_folder/some_folder/
There's an .htaccess file in /my_old_folder/ with the following code:
RewriteEngine on
RewriteRule ^my_old_folder/(.*) my_new_folder/$1
I want to rewrite the folder my_old_folder internally to my_new_folder, without changing the URL in the browser. Just grab the files from /my_new_folder/ instead of /my_old_folder/. If there's another folder like /some_folder/ in this case, keep it. Only change the name /my_old_folder/ to /my_new_folder/.
Unfortunately, it's not rewriting the path, although I already tried many solutions from the internet, including the above one.
Who can help?
Inside /my_old_folder/.htaccess you can use this rule:
RewriteEngine on
RewriteRule .* /my_new_folder/$0 [L]
It is because all path matching is relative to my_old_folder/ inside /my_old_folder/.htaccess.

.htaccess regex

I'm a real beginner with regex. I think I NEARLY understand this, but having spent a day and a half on it, I can't quite get it right.
At the moment my OLD domain has this rewrite:
RewriteRule ^/?$ "http\:\/\/sheepdog\-training\.com\/" [R=301,L]
That successfully redirects every page on the old site to the exact same file on the new site (sheepdog-training.com). Unfortunately, I need it to redirect to two new sites.
I need all files which begin with /tb- to go to: sheepdog-training.com and I need (nearly) all the remaining files to go to: theworkingsheepdog.com.
The exceptions are: contact and main-menu.
My web hosting company says I should do a separate 301 redirect for every page and post on the website, but there are hundreds of them! I'm sure what I'm looking for is possible with regex. Can you help please?
You will have to have multiple RewriteRules (remember that the rewrite conditions apply to the rule that immediately follows them thought).
Also remember: You should avoid using .htaccess files completely if you have access to httpd main server config file. Using .htaccess files slows down your Apache http server. Any directive that you can include in a .htaccess file is better set in a Directory block, as it will have the same effect with better performance.. See here: https://httpd.apache.org/docs/current/howto/htaccess.html
RewriteRule ^/tb-(.*)$ "http\:\/\/sheepdog\-training\.com\/$1" [R=301,L]
RewriteRule ^/?(.*)$ "http\:\/\/theworkingsheepdog\.com\/$1" [R=301,L]
Something like this should redirect all the paths starting with /tb- to the same path (except for the /tb- part) to the sheepdog-training site. And the other rule should redirect all the other URLs to the workingsheepdog site. If the first rule matches, the rule processing stops, so order of these two rules is important.
Configs are not tested by me, but should require only small modifications if any in case they do not work.

How to redirect from specific subdirectory to a subdomain via .htaccess?

I've been trying to redirect this URL (and all its substructures):
http://example.com/archive/
to (and its corresponding substructures):
http://archive.example.com/
For example: http://example.com/archive/signature/logo.png ==> http://archive.example.com/signature/logo.png
I tried to generate an .htaccess rule using a generator and evaluating it by looking at the regex, which I can understand (I think).
The result was the following rule:
RewriteEngine On
RewriteRule http://example.com/archive/(.*) http://archive.example.com/$1 [R=301,L]
The way I see it, the server will proccess any URL that starts with http://example.com/archive/ , will capture the string that comes next and will change the whole initial portion with the subdomain structure and append the captured string.
Unfortunately, this doesn't seem to work neither on my server, nor on online testing tools such as: http://htaccess.madewithlove.be/
Is there anything I'm missing there?
Thank you!
You should be able to try it this way.
RewriteEngine On
RewriteRule ^archive/(.*)$ http://archive.example.com/$1 [R=301,L]
Note that I did not make it dynamic as you didn't specific if you will have more URL's that need to work this way as well or not.

Apache .htaccess redirects for URL's with Arabic characters

I have been trying to add some .htaccess redirection rules for URL's with parts in Arabic. Obviously, I am using the Arabic part, URL encoded, but still Apache seems to not be able to resolve the URL, either catch it in the condition or redirect it. Does anyone have any experience on the subject?
This is one line from the config file
RewriteRule ^arabic/%D8%AE%D9%85%D8%B3-%D8%B7%D8%B1%D9%82-%D9%84%D8%B2%D9%8A%D8%A7%D8%AF%D8%‌​A9-%D8%B3%D8%B1%D8%B9%D8%A9-%D8%A7%D9%84%D8%A8%D8%B1%D9%88%D8%AF%D8%A8%D8%A7%D9%8‌​6%D8%AF(.*)$ /blog/sa-ar/%D8%AE%D9%85%D8%B3-%D8%B7%D8%B1%D9%82-%D9%84%D8%B2%D9%8A%D8%A7%D8%AF‌​%D8%A9-%D8%B3%D8%B1%D8%B9%D8%A9-%D8%A7%D9%84%D8%A8%D8%B1%D9%88%D8%AF%D8%A8%D8%A7%‌​D9%86%D8%AF/$1 [R=301,L]
I had the same problem with "م" which is "%D9%85"
Just use this:
RewriteRule ^arabic/([\s\S]*)$ /blog/sa-ar/$1
that worked for me!
In fact you have to use ^([\s\S]*)$ instead of ^(.*)$

Add exception to htaccess redirect

I have the following in my htaccess:
RewriteRule ^news/[a-zA-Z0-9-_]+?/(.+)$ /news [R=301,L]
This works how I want it to, for example if I go to /news/some-category/some-post it just redirects to /news which is great. However it is also affecting my uploads folder. Take the following URL for example:
/news/wp-content/uploads/2014/07/Emily-286x300.jpg
This gets redirected to /news as well so all my images are broken. Is there a way to tweak this rule so that it doesn't affect the wp-content/uploads directory?
If /news/some-category/some-post represent a 3-level subdirectories, you can use this rule
RewriteRule ^news/[^/]+/[^/]+/?$ /news [R=301,L]
Also, please note you'll have to clear your browser's cache before trying again such link /news/wp-content/uploads/2014/07/Emily-286x300.jpg (my rule will not match it but your last rule does and it's in browser's cache)