.htaccess Wildcard Redirect With GET Variables - .htaccess

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.

Related

htaccess RewriteRule no redirect, first rule works, second not

I have no clue why my first rule works, but my second not.
I want to change several existing Folders on my webspace to use php scripts in other Folders. no redirect.
https://url.com/test/this-is-a-nice-site/ should load php from https://url.com/media/this-is-a-nice-site/
RewriteEngine on
RewriteRule ^test/testT1/(.*)$ /media/testt1/$1 [L]
RewriteRule ^test/this-is-a-nice-site/(.*)$ /media/this-is-a-nice-site/$1 [L]
The First Rule works, when I go to https://url.com/test/testT1/ I get the files/php from /media/testt1/
But when I try https://url.com/test/this-is-a-nice-site/ , I get a 301 redirect to https://url.com/media/this-is-a-nice-site/
I don't get why, the Rule is the same, both Folders test/testH3/ and test/this-is-a-nice-site/ exists.
I couldn't find anything that helps.

Try to Fix 301 Redirect of a Dynamic URL | Not Working

I'm trying to 301 redirect the URL /topics/blog/dot-net/?page_id=386 to http://www.example.com/dot-net.
I have tried like below:
Redirect 301 /topics/blog/dot-net/?page_id=386 http://www.example.com/dot-net
Above will not work i was sure but just tried.
And this one:
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /topics/blog/dot-net/?page_id=386(.*)\ HTTP
RewriteRule ^ /dot-net? [R=301,L]
If you know something about this issue. How i could fix this in .htaccess please give me a suggestion.
A mod_alias Redirect cannot be used to match against the query string. Your mod_rewrite attempt potentially should work. There might be a caching issue (from previous failed attempts). However, if you just want to redirect the stated URL then it can be written something like:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^page_id=396$
RewriteRule ^topics/blog/dot-net/$ /dot-net? [R=302,L]
It is more efficient to check the URL-path in the RewriteRule pattern, rather than use a generic catch-all.
You would only need to check against THE_REQUEST if you needed to prevent a rewrite loop - but this is not stated in your question.
Change the 302 (temporary) to 301 (permanent) when you are sure it's working OK.

htaccess rule is true but site gives 404

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]

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.

301 Redirect via .htaccess php?i2=sajsak to productname.html - Not blanket

OK, so I'm trying to do a straight redirect:
store.php?StoreLevel=3&PrevStoreLevel=1&productid=6067&Level1=155&p=5&term=&BrowseBy=0
Redirected to:
mynewproductname.html
I'm not looking for a once size fits all rewrite rule, each one of these has a corresponding .html counter part, so kinda "hardcoded"
These are for 301's and Google Rankings
I can't figure this out. All resources I have found give me blanket rules on how to convert all php links in one shot to a html counterpart with the same name. I'm not looking to do this.
I've tried many things, none work. Some of my attempts:
RewriteRule ^store.php?StoreLevel=3&PrevStoreLevel=1&productid=6067&Level1=155&p=5&term=&BrowseBy=0 $ mynewproductname.html [R=301,L]
RewriteCond %{QUERY_STRING} =store.php?StoreLevel=3&PrevStoreLevel=1&productid=6067&Level1=155&p=5&term=&BrowseBy=0
RewriteRule mynewproductname.html [R=301,L]
RewriteCond %{QUERY_STRING} ^StoreLevel=3&PrevStoreLevel=1&productid=6067&Level1=155&p=5&term=&BrowseBy=0$
RewriteRule ^store.php$ http://mysite.com/mynewproductname.html? [R=301,L]
Some of these cause a 500 server error, some just don't work. Assistance is greatly appreciated.
EDIT - mod_rewrite is on and working fine, was able to redirect:
RewriteRule ^foo$ http://mysite.com/testing.html [R=301,L]
With success
Instead of using .htaccess, you could try redirecting from within your .php file. Have a lookup table of productid => webpage that gets scanned when the .php is first loaded; if you get a match, return a 301. If you don't match, continue on with regular .php processing (or 404, as appropriate)

Resources