Lets say I have a two websites www.sample.com and files.sample.com.
In an .htaccess file within the webroot of www.sample.com, I have the following:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{QUERY_STRING} ^files\/uploads [NC]
RewriteRule ^(.*)$ http://files.website.com/$1 [NC,L]
</IfModule>
The desired result is to have all requests of www.sample.com/files/uploads/file.xml or www.sample.com/files/uploads/subfolder/file.json get 302 redirected to files.sample.com/files/uploads/file.xml and www.sample.com/files/uploads/subfolder/file.json, respectively.
However, I can't get the rule to fire. The directory "files" does not exist on the www.sample.com website at all.
Could anyone give me a little help as to why the
You probably want REQUEST_URI not QUERY_STRING.
RewriteCond %{REQUEST_URI} ^/files\/uploads [NC]
Also note the leading slash.
Did you turn on mod_rewrite in your apache config?
Also yor change '^files/uploads' to ^/files/uploads and QUERY_STRING to PATH_INFO.
QUERY_STRING - this is all data after '?' character.
Related
I want to redirect https://senturia.com.vn/senturia-nam-sai-gon/?gallery=170
to https://senturia.com.vn/senturia-nam-sai-gon/projects/nha-pho-thuong-mai-5mx12m/
I tried this .htaccess file
<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} gallery=170 [NC]
RewriteRule (.*) /senturia-nam-sai-gon/project/nha-pho-thuong-mai-5mx12m/? [R=301,L]
</IfModule>
But it's not work
Your configuration looks fine. I highly recommend making sure that mod_rewrite is enabled on the server. Also, make sure that the .htaccess file is located in the correct place. If your are using some CMS with a single index.php file, the .htaccess should sit on the same level as that file. If senturia-nam-sai-gon is an actual directory on your server with its own index.php file, then the .htaccess should sit in that folder.
Here is how I would do it in a CMS:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (?:^|&)gallery=170 [NC]
RewriteRule ^senturia-nam-sai-gon/ /senturia-nam-sai-gon/project/nha-pho-thuong-mai-5mx12m/? [R=301,L,NC]
</IfModule>
Detailed break-down:
RewriteEngine on simply enables URL rewriting
RewriteBase / sets the base of all URLs (in this case simply /)
RewriteCond %{QUERY_STRING (?:^|&)galler=170 [NC] applies the following rule, if the GET parameters contain gallery=170. The [NC] flag makes the condition ignore upper- and lower-case
RewriteRule ^senturia-nam-sai-gon/ /senturia-nam-sai-gon/project/nha-pho-thuong-mai-5mx12m/? [R=301,L,NC] redirects /senturia-nam-sai-gon/ to /senturia-nam-sai-gon/project/nha-pho-thuong-mai-5mx12m/ with a status-code of 301 ("Moved permanently"). The ? at the end of the rule removes the querystring entirely. The L flag makes sure this is the last rule that gets applied. The NC flag does the same as before.
I have a site where I need to show one URL for SEO reasons, but the actual landing page is a slightly different URL. I originally thought the requirement was to show the url which actually exists, but I was incorrect.
The required url: www.somesite.com/people/johndoe/?id=10
The url which actually exists: www.somesite.com/people/?id=10
I am trying this in my .htaccess file but to no avail:
RewriteRule ^/person/.+$ /person/$1 [R=301.L]
This doesn't appear to change anything and I am told my url doesn't exist.
I have looked up many similar questions on this site and elsewhere, but cannot find a solution that works. Thanks in advance
Edit (existing .htaccess):
# php_flag magic_quotes_gpc Off
# 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
#AuthName "username"
#AuthName "username"
#AuthUserFile "/home/something/.htpasswds/public_html/passwd"
#require valid-user
AuthName "username"
AuthUserFile "/home/something/.htpasswds/public_html/passwd"
RewriteCond %{HTTP_HOST} ^sitename.org$
RewriteRule ^(.*)$ "http\:\/\/www\.sitename\.org\/$1" [R=301,L]
For doing internal rewrites use mod_rewrite rules with using R flag:
Use this code in your root .htaccess:
RewriteEngine On
RewriteRule ^(people)/.+$ /$1 [L,NC]
Your rewriterule condition cannot include a leading /. It is stripped off. Use
RewriteRule ^person/.+$ /person/$1 [R=301,L]
Also note that you had a period in the flags, not a comma.
Lose the " and \'s in the last rewrite rule:
RewriteRule ^(.*)$ http://www.sitename.org/$1 [R=301,L]
I don't know if you're going to get in any trouble with multiple 301s in action. It may depend upon the server configuration. Do you need for the revised URI to go back to the visitor and be seen in their browser? If not, lose the 301 on that rewrite rule.
Document what you're doing and why, with comments. For example, your last two lines are to force www.sitename.org instead of sitename.org.
I needed to redirect "example.com/image/img.jpg" to "example.com/view/img.jpg"
Therefore I've used
RewriteRule (^|.*?/)image/(.*)$ /$1view/$2 [R=302,L,NC]
But, still if I try to visit "example.com/image/img.jpg" it won't redirect. What am I doing wrong?
Thank you.
I needed to redirect "example.com/image/img.jpg" to "example.com/view/img.jpg"
Apparently the above examples are not accurate as your rule shows directories image and view can be at any level in the corresponding URL directory structures.
If that's the case, you may try this in one .htaccess file at root directory:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(.*)?image/([^.]+)\.jpg [NC]
RewriteRule .* /%1view/%2.jpg [R,L,NC]
In case image and view directories are indeed at the first level as described in the examples, replace the last 2 lines with this one:
RewriteRule ^image/(.*) /view/$1 [R,L,NC]
Replace [R,L,NC] with [R=301,L,NC] for permanent redirection or with [L,NC] for internal mapping.
Preface
I'm trying to re-write a URL for a profile page. All of my application pages have a .html extension, so I'm trying to match just letters, numbers, -, and ..
So these would be valid
site.com/steve
site.com/steve-robbins
site.com/steve.robbins
But these wouldn't be
site.com/steve.html
site.com/steve-robbins.php
Assume I have a check in place so that custom URLs don't have .html or .php on the end.
Problem
I'm currently using this but it's not working
RewriteRule ^([a-zA-Z0-9\.-]+)$ profile.php?url=$1 [L]
It should set url to steve, but it's setting it to profile.php
What am I doing wrong?
My complete .htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*) http://www.%{HTTP_HOST}/$1 [R=301]
#
# LOGIN
#
RewriteRule ^([a-z0-9]{255})/activate\.html$ login.php?activate=$1 [L]
RewriteRule ^logout\.html$ login.php?logout [L]
#
# SETTINGS
#
RewriteRule ^change-([a-z]+)\.html$ account-settings.php?$1 [L]
RewriteRule ^([a-zA-Z0-9\.-]+)$ profile.php?url=$1 [L]
# SEO friendly URLs
RewriteRule ^([a-zA-Z0-9-_.]+)\.html$ $1.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([a-zA-Z0-9-_.]+)\.php
RewriteRule ^([a-zA-Z0-9-_.]+)\.php$ $1.html [R=301]
Add this to the top of your rules (under the RewriteBase / directive):
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
That should stop it from looping. The rewrite engine will keep re-applying all the rules until the URI going in (sans query string) is the same as the URI that comes out of the rules. That's why the value of url is profile.php.
I'm kind of a beginner in interpreting mod_rewrite rules but if I understand it correctly your rule is matched and than matched again, either add something to the url matching scheme like /profile/user or add a condition to not redirect if already redirected
Try adding a leading slash to the redirect like this:
RewriteRule ^([a-zA-Z0-9.-]+)$ /profile.php?url=$1 [L]
The reason you're getting a url value of profile.php is because the [L] flag is kinda misleading when it comes to the .htaccess file. In the server config files it does exactly what you'd think, but in the .htaccess file it stops reading rules at that rule, but then goes through the rules again until path is unchanged by any of the rules. By adding the leading /, your rule will not match the second time around as you exclude / from the regex. I spent a while struggling with this feature myself.
I want to redirect /blog to /blog/. I have two condition if blog is in the url first condition will be called else second condition. Please anybody help me to do this.
I did this.
#blog
RewriteCond %{REQUEST_URI} ^/blog.*
RewriteRule (.*) /blog/$1 [L]
#squeezepage
RewriteEngine On
RewriteRule ^([0-9a-zA-Z]+)$ index.php?option=com_squeezepage&sqpage=$1 [NC]
Thanks.
You can do something like this to redirect /blog to /blog/ however if /blog is a physical folder then Apache does it automatically for you:
RewriteRule ^blog$ /blog/ [L,R=301,NC]
Do you only want the trailing slash for /blog requests or do you want it for all directories?
Because there is a directive called DirectorySlash on which might be what you're looking for.
See this question.