htaccess regex redirect - apache

I am trying to redirect from source url to target url using regex but it didn't work. Here i describe my problem -
Source URL:
http://example.com/forums/forum/lisa-goran-bygger-hus-t5075/
Target URL:
http://example.com/forums/details/lisa-goran-bygger-hus/
want to redirect without -t5075 from url.
htaccess rewrite rule is:
RewriteRule ^/forums/forum/([a-z-]+[^-t0-9]) /forums/details/$1 [R=301,L]
this isn't work its redirect to me -
http://example.com/forums/details/lisa-goran-bygger-hus-t5075/
Here regex online tester link -
http://www.regextester.com/?fam=97698
What i am doing wrong can someone help me out and explain about this problem.
Thanks

First, make sure mod_rewrite is enabled and htaccess files allowed to be executed.
Then, make sure .htaccess file is in root folder and look like this
RewriteEngine On
RewriteRule ^forums/forum/(.+)-t[0-9]+/$ /forums/details/$1/ [R=301,L]
Finally, try clearing your browser's cache. Indeed, your old rules could interfere. After that, it should work.

Related

Using Apache Rewrite Rule to pass information from URL to Filename

I've been banging my head against this one and have unfortunately failed. I'm hoping somebody can help me.
I'd like to pass information from a URL to a filename. Basically I'd like to do the following:
There is only one real file on the server:
http://www.domain.com/download/file.zip
If a user enters the following URL into his browser:
http://www.domain.com/download/file_xyx.zip I'd like apache internally serving file.zip but the user downloading the file as file_xyz.zip
xyz is intended to be a variable (of any length) so the rewrite rule should simply accept anything it's place and always internally link to the same file.
I'm thinking this can be achieved with a rewrite rule, am I wrong?
Yes, you just need this rule:
RewriteEngine On
RewriteRule ^/?download/file_[0-9]+\.zip /download/file.zip [L]
If you are using htaccess files, you can place this rule in an htaccess file in the "download" folder:
RewriteEngine On
RewriteBase /download/
RewriteRule ^file_[0-9]+\.zip file.zip [L]

Recursively redirect all PDF's with .htaccess rule?

I'm trying to come up with a rule that will redirect all PDFs to a different directory recursively, retaining the path aside from the change I'm making.
Initial directory is /wp-content/uploads
Target directory is /build/wp-content/uploads
So /wp-content/uploads/2013/03/LW_Stevens.pdf
Would redirect to /build/wp-content/uploads/2013/03/LW_Stevens.pdf
RewriteEngine On
RewriteRule ^wp-content/uploads/*/(.+\.pdf)$ build/wp-content/uploads/*/$1 [L]
Obviously the /*/ part is wrong, how would I do this correctly?
Here is my suggestion to match with or without a path after initial directory.
If there is no path after the initial directory, it will be ignored.
RewriteRule ^wp-content\/uploads\/(?:(.+)\/)?(.+\.pdf)$ build/wp-content/uploads/$1/$2 [L]
You can see the returned matches here:
http://rubular.com/r/16io6ZVSJc
Replace your rule with this:
RewriteRule ^(wp-content/uploads/.*?/[^.]+\.pdf)$ /build/$1 [L,NC]
And make sure this rule is placed above other standard Wordpress rules in your .htaccess.
Using a mix from both answers for wordpress.
Plugin: Redirection by John Godley
Redirection type: regex
origin url: ^(/pdfs/(?:(.+)/)?(.+.pdf))
destination url: URL$1

Use .htacess to redirect all files in a specific folder to a script

I want to use htacess to redirect the following examples:
www.site.com/downloads/file1.txt
www.site.com/downloads/folder/file2.txt
to
www.site.com/download?file=file1.txt
www.site.com/download?file=folder/file2.txt
Ignore that there are slashes in the query string for the sake of the example.
You can use mod_rewrite for that.
RewriteEngine on
RewriteRule "^/downloads/(.+)" "/download?file=$1" [R=301,L]
Set the proper HTTP status code for the rewrite action (301 - permanent, 307 - temporary).
As I never used mod_rewrite in .htacces, you may need to try out some more combinations. Maybe the directory name needs to be removed from the regular expression, as it is already clear from the .htaccess context - the manual should help.
RewriteEngine on
RewriteRule ^/downloads/(.*) /download?file=$1 [R=301,L]
301 - Moved Permanently
The resource has permanently moved to a different URI.
Resources:
HTTP Status Codes
http://www.helpwithpcs.com/courses/html/html_http_status_codes.htm#300
Apache mod_rewrite
http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Rewriting a redirected URL with mod_rewrite

Here is my setup :
I have a website located at www.cabsh.org/drupal
I want to use mod_rewrite to do 2 things :
Redirect www.cabsh.org to http://www.cabsh.org/drupal/index.php (I got this one)
Rewrite /www.cabsh.org/drupal/index.php to www.cabsh.org/site/index.php
I cannot figure how to achieve the 2nd point. I'm using .htaccess files since I cannot use the main server configuration. Can anyone help me getting this to work?
Thanks!
From what I get from your comment, you just want something like this:
RewriteEngine on
# Prevent a request directly to the /drupal folder
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/drupal/
RewriteRule ^drupal/(.*)$ /site/$1 [R=301,L]
# Change a request for /site/(anything) to /drupal/(anything)
RewriteRule ^site/(.*)$ /drupal/$1
Be careful though, since Drupal (being in the Drupal folder) might generate links that point to /drupal instead of /site, which is seemingly not what you want.

.htaccess Redirection for Querystrings

I have an old website that URLs were like /page.aspx?sch=XXXX&prg=YYYY. I want to redirect those old URLs to my new website's URLs. I'm trying to use an .htaccess file to do the trick but I couldn't get it work. What I want is:
/page.aspx?sch=XXXX&prg=YYYY ==> /page/sch/XXXX/prg/YYYY
If someone could helped me, I'd be very pleased.
Thanks...
If there are only these two URL parameters and they are only used in this order, you can do the following:
RewriteCond %{QUERY_STRING} ^sch=([^&]+)&prg=([^&]+)$
RewriteRule ^page\.aspx$ /page/sch/%1/prg/%2? [L,R=301]
Otherwise you will need to extract these parameters one by one and put them in in right order before doing that redirect.