I'm creating a static webpage solution for my angularJS project where I check for the HTTP_USER_AGENT in my .htaccess file and then redirect any crawlers to a static php page so I can add all the necessary meta data.
The problem is that I'm using a wildcard subdomain, so any of my clients can have their own subdomain and I can't figure out how to redirect via RewriteRule to the right url.
My RewriteCondlooks like this:
RewriteCond %{HTTP_USER_AGENT} (facebookexternalhit/[0-9]|Facebot|Twitterbot|Pinterest|Google.*snippet)
and my basic non-working RewriteRule looks like this:
RewriteRule ^stuff/(.*)$ http://example.com/static.php?token=$1 [NC,L]
But I want it to go to static.php on that subdomain the request is on, so if I request http://subdomain.example.com/stuff?token=my-token is it possible to make the rewriterule go to http://subdomain.example.com/static.php?token=$1 instead of just the static mydomain.com above?
(there will NEVER be a request on http://example.com/stuff - all requests to stuff will be via a subdomain)
I'm not that experienced in .htaccess and get's a bit confused from all the answer that here listed at SO and Google finds.
I solved the issue by adding together pieces of code and a little bit of thinking.
This is the three lines in my .htaccess which does the trick for me:
RewriteCond %{HTTP_USER_AGENT} (facebookexternalhit/[0-9]|Facebot|Twitterbot|Pinterest|Google.*snippet)
RewriteCond %{HTTP_HOST} ^(.+?)\.example\.com$
RewriteRule ^s/(.*)$ http://%1.sociuu.com/static.php?token=$1 [NC,L]
If this can be done more elegant or smarter, please let me know otherwise I'll consider this as suitable for my needs :-)
Related
By using WordPress with Divi and custom php pages/scripts I ended with urls looking like this:
https://example.com/?day=today
https://example.com/?sport=1&league=123 (1=football and 123=premier_league)
I want to make them more user and SEO friendly and rewrite them in htaccess to:
https://example.com/?day=today ==> https://example.com/today
https://example.com/?sport=1&league=123 ==> https://example.com/football/premier_league
For the "day" link I tried this code, but it doesn't work
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-z\-\_]+)?$ /?day=$1 [L,QSA,NC]
</IfModule>
For the other example with sport and league I wanted to find a solution in which I will manually add old link and new link, tried redirect 301, but I think it can't work, right?
Redirect 301 https://example.com/?sport=1&league=123 https://example.com/football/premier_league
I also tried Redirection plugin in WP, but it created Rule which destroys page and makes error 500:
RewriteRule ?sport=1&liga=123 https://example.com/football/premier_league [R=301,L]
or
RewriteCond %{QUERY_STRING} ^sport=1&league=123$
RewriteRule ^$ https://example.com/football/premier_league [R=301,L]
which gaved address: https://example.com/premier_league/?sport=1&league=123
I was also wondering if I can make it with pure php, since in PHP I can easilly get proper strings for id's of the sports and leagues. Unfortunantelly changing "?sport=1&league=32758" into "?sport=football&league=premier_league" and then clean it with rewrite in htacces is not possible right now.
I'm not expecting whole code for both problems, I believe that some directions would be enough. I browse stack and google and coudn't find any examples with links that looks like mine, so example.com/?sport=1 not example.com/page?sport=1
After further investigation I believe the problem is in the way I'm opening my custom pages. I'm using shortcodes to initiate a script and inside the script my php code is included with parameters. I think that because of that I got to this page https://example.com/premier_league/?sport=1&league=123 which was pretty close, but not working.
Now my question is how to send those parameters to proper script but not into url.
Long time reader first time poster.
I have read through what feels like 100's on similar posts trying to make it fit what i am trying to do.
Apologies if its been covered. I believe I have the correct code but it maybe conflicting with something else?
I have old URLs from an old ASP cart and want to 301 redirect to new magneto static pages.
There are only 5 of these URLS so don't need a bulk action.
example:
OLD URL: http://test.com.au/shopcontent.asp?type=FAQ%20and%20Terms
NEW URL: http://test.com.au/privacy-policy-cookie-restriction-mode
I know that from reading other posts you can not simply use:
Redirect 301 /shopcontent.asp?type=FAQ%20and%20Terms /privacy-policy-cookie-restriction-mode
So I have worked out that this should work:
RewriteCond %{QUERY_STRING} ^type=FAQ%20and%20Terms$ [NC]
RewriteRule ^shopcontent\.asp$ /privacy-policy-cookie-restriction-mode/? [NC,R=301,L]
I have also tried removing the %20 for the spaces like this:
RewriteCond %{QUERY_STRING} ^type=FAQ\ and\ Terms$ [NC]
RewriteRule ^shopcontent\.asp$ /privacy-policy-cookie-restriction-mode/? [NC,R=301,L]
When I try and access the old URL it just goes to the 404 page. I am using standard .htaccess file that came with Magento (I can post if that helps);
I am using a VPS which mod rewrite is enabled. I have other Redirect 301's for non dynamic URLS.
Are there any logs that I can look at?
Appreciate any help or advice.
Thanks Rudi
ps. I found this post which got me to where i am now
RewriteCond %{QUERY_STRING} in mod_rewrite (dynamic to static URL) not working
You cant use this rule:
RewriteEngine On
RewriteCond %{QUERY_STRING} "^type=FAQ\%20and\%20Terms$" [NC]
RewriteRule ^shopcontent\.asp$ /privacy-policy-cookie-restriction-mode/? [NC,R=301,L]
TL; DR: I would like to hit the index-api.php file if api is found in the URL, but then simply keep all other requests pointing to the site/dist directory as if it were the 'root' of the site.
So, I've spent way too many hours on this and trust me, I've dug through all of the resources for mod_rewrite. I guess I'm just not quite understanding and figured I'd ask on here.
What I want to do, in theory, seems simple. I'm building a single page application (Angular App) using Grunt, outputting that to a the root of a WordPress install. The WordPress install is simply serving up an API using the WordPress JSON API plugin, so I want the root of the site to hit my Grunt directory (located at site/dist/index.html), but all requests to siteurl.com/api to hit the index.php file and proceed normally.
Keep in mind I have other assets / images located in this site/dist directory, so ideally, it would be awesome if all requests to the site root would simply use this folder as the "base" of the site (e.g. a request to siteurl.com/images/testimage.jpg pulls from site/dist/images/testimage.jpg).
I feel like I'm onto something here and am surprised I couldn't find anything that directly tackles this issue.
What I've done now is renamed the index.php from WordPress to index-api.php and left it the same:
index-api.php:
<?php
define('WP_USE_THEMES', true);
/** Loads the WordPress Environment and Template */
require('./wordpress/wp-blog-header.php');
// phpInfo();
.htaccess:
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/(.*)$ index-api.php [L]
RewriteRule (.*)$ site/dist/index.html [L]
</ifModule>
I tried a myriad of other efforts from a few posts trying to get this working, and it seems to me like it should work fine. The funny thing is, if I comment out the last line RewriteRule (.*)$ site/dist/index.html [L] the api request works normally as expected, so I know I'm close.
Any suggestions?
Would appreciate anyone's help on this, it's been really confusing!
In the first place you'll need to make sure that requests made to /index-api.php are not matched and rewritten by the second rule. In the second rule you can use $1. $1 will be replaced with whatever was matched in the first capture group. We'll also need to make sure that the second rule will not match what it rewrites, or we'll end up with an infinite loop and an internal error.
You can use the $1 in the first rule too, as I show below:
RewriteRule ^api/(.*)$ index-api.php?url=$1 [L]
RewriteCond %{REQUEST_URI} !^/site/dist/
RewriteCond %{REQUEST_URI} !^/index-api\.php
RewriteRule (.*)$ site/dist/$1 [L]
I recommend reading the documentation of mod_rewrite to get a better understanding how you can use it and what things you have at your disposal while rewriting url's.
I'd like to make shortened links for my site to be used in Tweets. I'm interested in a t.co-like URLs but confused on how to implement the redirect.
Here's how a link on my site typically looks:
https://mysite.com/item/this-is-a-book-on-toasters
Here's how I'd like the shortened link to look which would redirect to the above link
https://ms.co/Im8y2x
Based on googling how to do this, it looks I need to do a 301 redirect.
I'm using PHP, specifically Codeigniter and I guess there is 2 components: the PHP script and .htaccess.
Here's my .htaccess:
RewriteEngine on
RewriteRule ^/?$ https://mysite.com [L]
RewriteRule ^(.*)$ https://ms.co/$1 [R=301,NC]
The PHP I think I need is in here.
Unfortunately, I can't interpret the answers on this link to make a useful script. Might someone help with this? Also, does my .htaccess look right?
This should be the .htaccess code on your shortlinking website (ms.co):
RewriteEngine On
RewriteRule ^(.*)$ https://mysite.com/in.php?id=$1 [R=301,L]
The in.php should contain the script that decodes the $_GET['id'] (via the short hash decoding methods supplied in the link you supplied), matches it against an ID into your database, and retrieves page that it should redirect to.
By the way, the reason I didn't add a NC part in the code is because upper/lowercase (often) can yield different results when using decoding methods.
I've anaswered my own question. Here's what they .htaccess directive should look like given the question above:
RewriteCond %{HTTP_HOST} ^o-a\.co$ [NC] // the rewrite rule
RewriteCond %{HTTPS} =on //to enable HTTPS
RewriteRule ^(.*)$ https://mysite.com/page-to-handle-hash/$1 [L] //where in your application you want to send the 6-digit hash
I'm a novice php/mysql developer and need some help.
I was working on a project with a .NET developer. He built a URL redirecting function in .NET that uses the ? in the URL (eg, mysite.com?123 redirects to www.realsite.com. This developer has now disappeared on me and I no longer have access to the server.
I need to move my domain to a new server and recreate this functionality in php/mySQL. I have about 40 urls that I need to support using this technique (they're embedded in QR codes). I don't need a solution that generates new redirects this way - I've got a different solution going forward.
My new server will be on bluehost.
Many thanks to anyone who can help me.
To replicate the original setup you dond't need to involve PHP at all. You can accomplish this at the server level using some RewriteRules:
RewriteCond %{QUERY_STRING} ^123$
RewriteRule ^$ http://realsite.com/
See also http://wiki.apache.org/httpd/RewriteQueryString
The query string can be found in $_SERVER['QUERY_STRING'].
On your old host for mysite.com domain create a .htaccess file in DOCUMENT_ROOT like this:
Options +FollowSymlinks -MultiViews
RewriteEngine on
# for HTTP traffic
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{SERVER_PORT} =80
RewriteRule ^/?$ http://www.realsite.com/? [R=301,L]
# for HTTPS traffic
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{SERVER_PORT} =443
RewriteRule ^/?$ https://www.realsite.com/? [R=301,L]
Above code will redirect every URI that looks like mysite.com?foo to http://www.realsite.com/ with HTTP status code 301.
? in the end will discard any existing QUERY string.
http://example.com?123
would be the equivalent of doing
http://example.com/index.php?123
Your choices would be either to catch those codes via mod_rewrite and redirect the request to a script specifically set up to handle these legacy urls. Or you can modify the index.php script to look for the codes and do whatever it has to at that point.