htaccess rule is true but site gives 404 - .htaccess

I'm struggling to see why this rule won't work on the site.
The old url was /mobile/article/site/article-name and the new url is just /article/article-name
The rule is
RewriteRule ^mobile/article/tgc/(.*)$ /article/$1 [L,R=301]
Testing against http://htaccess.madewithlove.be/ with the whole htaccess file tells us
This rule was met, the new url is http://site.dev/article/article-name
Test are stopped, because of the R in your RewriteRule options. A redirect will be made with status code 301
but when we try hit http://site.dev/mobile/article/site/article-name in the browser, we get a 404 rather than a redirect.
What's even more curious is that we have the same kind of rule for another url
RewriteRule ^resources/a/(.*)$ http://resources.site.dev/library/$1 [L,R=301]
Which comes after our troubled redirect and it works just peachy.
What are we missing?

Keep your rule as this:
RewriteRule ^mobile/article/site/(.*)$ /article/$1 [L,NC,R=301]

Related

redirect 301 not working properly in .htaccess

htaccess 301 redirect. I have this old site which is for example http://test.org/conference I want to redirect it to conference.test.org
What happened is the 301 redirect in my .htaccess file is quite buggy.
Here is my 301 redirect htaccess code below:
RewriteCond %{REQUEST_URI} ^/http://test.org/conference(/)?
RewriteRule ^(.*)$ http://conference.test.org/? [R=301,L]
When I test this one it runs and redirects correctly. But when I test it over and over again. It seems not to redirect anymore.
Can someone help me have a htaccess 301 redirect code?
Any help is much appreciated.TIA
I assume this is what you are looking for:
RewriteEngine on
RewriteRule ^conference/?$ http://conference.test.org/ [R=301,L,QSA]
Please note that %{REQUEST_URI} only contains the path of the URI, so not the protocol and the hostname. Reason is that the evaluation is performed inside a http host. This is explicitly pointed out in the documentation: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Depending on your http hosts setup you might also have to add a condition to prevent an endless redirection loop:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^conference\.test\.org$ [NC]
RewriteRule ^conference/?$ http://conference.test.org/ [R=301,L,QSA]
But usually that is not required, since the rule should be defined inside the test.org http host...

.htaccess Wildcard Redirect With GET Variables

I am trying to redirect all of the following URLs on my site to a new version. Here is the goal:
BEFORE (Notice that there is no '.php')
https://example.com/view?username=asdf1234
REDIRECTS TO (With '.php' and inside the 'tool' folder)
https://example.com/tool/view.php?username=asdf1234
I've tried a couple examples but can't figure it out. This is my best guess
RewriteEngine On
RewriteRule ^/view\?username=(.+) /tool/view.php?%1 [L,R=301]
Adding
RewriteEngine On
RedirectMatch ^/view(.*)$ https://example.com/tool/view.php$1
Successfully redirected but it is a 302 redirect. When I added [L,R=301] it gave a 500 error.
You could try a negative assertion:
RewriteEngine On
RewriteCond "%{REQUEST_URI}" ^/tool/?.*$
RewriteRule ".?" "-" [S=1]
RewriteRule "^/(.*)$" "/tool/$1?%{QUERY_STRING}" [L,R=301]
The first rule states a condition that the URL begins with /tool.
The second rule states that if the first rule passes, SKIP the next 1
rule.
The third rule says to implement a redirect to /tool/(TheUrlYouVisited.whatever)
So, if the URL does not begin with /tool, the third rule will not be skipped, and thus you have your catch all redirect.
As a test, you could also just add a simple:
RedirectMatch ^/view(.*)$ http://www.example.com/tool/view$1
If that doesn't work, you know something else entirely is going on.

Need .htaccess assistance

We have an existing site, let's call it ourdomain.com. We moved content over from a site that is shutting down, let's call it legacydomain.com. We pointed the legacy domain at our server to the /public_html/ folder.
What I need is an .htaccess that will redirect legacydomain.com or legacydomain/anything-here to ourdomain.com/legacydomain/ with nothing else appended to the URL.
However, I also need a few specific URLs to redirect to certain destinations, and they don't really follow a pattern. For example:
legacydomain.com/something.html to ourdomain.com/legacydomain/something.html
legacydomain.com/another.html to ourdomain.com/legacydomain/folder/another.html
This is what I have tried:
RewriteCond %{HTTP_HOST} ^www\.legacydomain\.com$ [NC]
RewriteRule (.*) http://www.ourdomain.com/legacydomain/$1 [R=301,L]
Redirect 301 /something.html http://www.ourdomain.com/legacydomain/another.html
It mostly works, but if I visit legacydomain.com/anything-here it doesn't even attempt to rewrite, it just keeps the domain the same and gives a 404. And also I have a feeling that even if it did work, something like legacydomain.com/anything-here/more-stuff would get rewritten as ourdomain.com/legacydomain/anything-here/more-stuff which I don't want.
Only other thing in the .htaccess is rewriting non-www to www, and the standard WordPress stuff. Any guidance would be greatly appreciated. Everything above should have an http:// and www in front for the examples, but it wouldn't let me post that many "links".
For each specific rewrite you would need two lines, as follows. Depending on your existing config you may need to add a slash at the beginning of the RewriteRule in front of something.html if this doesn't work.
RewriteCond %{HTTP_HOST} legacydomain.com
RewriteRule something.html http://ourdomain.com/legacydomain/something.html [R=301,L]
Then you would use a catch-all for everything else.
RewriteCond %{HTTP_HOST} legacydomain.com
RewriteRule (.*) http://ourdomain.com/legacydomain/ [R=301,L]
Personally, I would go for the simplest solution which doesn't use mod_rewrite. First, just redirect the specific pages to wherever they need to go.
Redirect 301 /something.html http://ourdomain.com/legacydomain/something.html
Redirect 301 /another.html http://ourdomain.com/legacydomain/another.html
Then, simply redirect everything else to the base URL.
RedirectMatch 301 (.*) http://ourdomain.com/legacydomain/
These must be put in your .htaccess file before the RewriteEngine on statement.

.htaccess RewriteRule works but the URL isn't changing in the address bar?

I have been pulling my hair trying to figure this out but nothing is working. I have a webpage at mysite.com/test.php I want to do a simple URL rewrite and change it to mysite.com/testRewrite/ The code to do implement this should be:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^testRewrite/$ test.php [NC,L]
For whatever reason it's just not working. When you go to the new URL - mysite.com/testRewrite/ it works.
However, if you type in the original URL mysite.com/test.php it doesn't change the address to the new URL in the browser.
mod_rewrite won't automatically enforce redirecting browsers from rewritten URLs. The rule you have simply says "if someone asks for /testRewrite/, internally change it to /test.php". It does nothing to handle actual requests for /test.php, which is why when you try to access mysite.com/test.php, it gives you /test.php.
Now, the problem with mod_rewrite is if you simply add that rule:
RewriteRule ^test.php$ /testRewrite/ [L,R=301]
which will redirect the browser when it asks for /test.php to /testRewrite/, the first rule will be applied after the browser redirects, and the URI gets rewritten to /test.php, then it goes back through the entire rewrite engine and the above rule gets applied again, thus redirecting the browser, thus the first rule rewrites to /test.php, and the above rule redirects again, etc. etc. You get a redirect loop. You have to add a condition to ensure the browser *actually requested /test.php and that it's not a URI that's been churned through the rewrite engine:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /test\.php [NC]
RewriteRule ^test.php$ /testRewrite/ [L,R=301]
This way, after the 301 redirect happens, this rule won't get applied again because the actual request will be GET /testRewrite/ HTTP/1.1, and the condition won't be met.

Hide index.php from web site URL

I'm using htaccess rewrite engine to make urls look nice,
from www.mysite.com/index.php?pag=home to www.mysite.com/pag/home
it works fine with this rule
RewriteRule ^pag/([^/]+)$ index.php?pag=$1 [L,QSA,NC]
but when I go to www.mysite.com it redirects me to www.mysite.com/index.php
is there a way to redirect to www.mysite.com/pag/home?
I tried
redirect 301 /index.php http://www.mysite.com/pag/home
but when i try to go to www.mysite.com the browser gives my "page do not exsist error"
This is the rule that Drupal uses to make it's pretty urls, yours should be extremely similar:
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Your rule only matches on urls that have pag in them, but you're really wanting to hit every url.
the problem was in this redirect rule
redirect 301 /index.htm http://www.mysite.com/index.php

Resources