Completely fake URL with .htaccess - .htaccess

I am serving static content with Amazon CloudFront and am using my servers as the origin.
Since CF does not respect ? query strings, I can't easily force CF to use new versions of .png files. I also need fast invalidation and do not want to pay for invalidation requests.
So I want to create a completely fake directory with .htaccess to force versioning for my images.
For instance, I want:
domain.com/static/0.12/images/background.png
going to
domain.com/static/images/background.png
Where 0.12 is my APP version, which will be automatically changed through my deployment script.
What would be the .htaccess rule for this?

If you actually want the URL
domain.com/static/0.12/images/background.png
to be internally redirected to
domain.com/static/images/background.png
Add the following to the .htaccess file in the root of your site.
RewriteEngine On
RewriteBase /
RewriteRule ^static/0\.12/images/(.+)$ static/images/$1 [L,NC]
If the APP version varies then use
RewriteEngine On
RewriteBase /
RewriteRule ^static/[\.0-9]+/images/(.+)$ static/images/$1 [L,NC]

To force all requests from a URL like
example.com/static/images/background.png
to rewrite to internal URLs like
example.com/static/0.12/images/background.png
Use:
RewriteEngine On
RewriteRule ^static/images/(.*)$ static/0.12/images/$1 [L]

Related

Rewrite a complex link .htaccess

My old website have product link like this
domain-name/index.php/product-category/detail/603/product-name.html
my new Wordpress website have product link like this
domain-name/product-category/product-name
i config in .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^product-category/detail/([0-9]+)/([a-zA-Z-]*).html /product-category/$2 [QSA,L]
in permalink i config:
/%category%/%postname%
The new site works very correctly, but the old link is not working. Old link always redirect to error 404 absolute, i want to keep old link on google auto redirect to new link. Does anyone have a solution for this please, thank u very much.
This should be your rule modified to actually match the requested URL you show:
RewriteEngine On
RewriteRule ^/?index\.php/product-category/detail/\d+/([\w-]+)\.html /product-category/$2 [QSA,END]
But more likely you want the more flexible variant:
RewriteEngine On
RewriteRule ^/?index\.php/([\w-]+)/detail/\d+/([\w-]+)\.html /$1/$2 [QSA,END]
You should prefer to implement such rules in the actual http server's host configuration. If you have no access to that you can use a distributed configuration file (often called ".htaccess"), but that comes with some disadvantages.
I could imagine that it also makes sense to send an external redirection to clients still requesting those old URLs:
RewriteEngine On
RewriteRule ^/?index\.php/([\w-]+)/detail/\d+/([\w-]+)\.html /$1/$2 [QSA,R=301,END]
For that it might make sense to start out with a R=302 temporary redirection and to change that to a R=301 permanent redirection once you checked that everything works as expected. That prevents annoying caching issues...

.htaccess root url to subfolder, and specific url to root folder

My setup to weird, needless to say im particularly limited in terms of what i can change. At the moment, the easiest thing for me to change is the server's .htaccess file. The current directory setup looks like:
/ is a wordpress installation
/front-end is a react website that uses the wordpress installations REST API to get data
I am trying to get it so that when i go to example.com it serves content from /front-end but all requests to /dashboard are served from the root directory.
For example these URLs would do the following:
/ and /contact would point to the index.html file inside the front-end directory (react-router would pick up the /contact)
/dashboard/wp-admin and /dashboard/wp-json/menus would let wordpress in the route directory take over. (these would ALWAYS start /dashboard)
Is what im trying to achieve doable?
I have this to start with, but im unsure how to ignore the /dashboard route
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-s
RewriteCond %{DOCUMENT_ROOT}/front-end/%{REQUEST_URI} -s
RewriteRule . /front-end/index.html [L]
RewriteEngine On
RewriteRule ^/?dashboard/(.*)$ /$1?wordpress=true [L,NC]
RewriteCond %{QUERY_STRING} !^.*wordpress=true.*$ [NC]
RewriteCond %{REQUEST_URI} !^/?front\-end.*$ [NC]
RewriteRule ^(.*)$ front-end/$1 [L,NC]
In the above rules, we first check if we have dashboard in our requests. If yes, we deliberately attach a query string wordpress=true to avoid confusion with your react folder and run into a too many redirects issue.
This way, we also know that the current request is for wordpress and further rewrite rules should not touch it.
In the second rewrite rule, we check the conditions as to whether we have wordpress=true or not and see if this does not already have front-end in the URL(also applicable for internal routing and need not be explicitly specified). If it passed both tests, we redirect it to your react front-end, else we leave it to execute as is.
Reason to attach !^/?front\-end.*$ is to avoid infinite internal redirects to itself.

Remove hash in URL using htaccess file

In Previous my application was single page application. so we used id's in the URLS.
Currently we have changed as seperate pages. but our site URL is in external site. So we have to redirect the URL lines in the htacces file
www.mydomain.com/#news/
to
www.mydomain.com/news/
how to achieve this?
Try using this:
RewriteEngine On
RewriteBase /
RewriteRule ^#/(.*)$ /$1 [L,NC,R]
This should remove the # from your URL. Just make sure you clear your cache before testing this.
Once you're happy that the rewrite is working, you can change R to R=301. Don't do this beforehand.

htaccess redirect if url contains word on changed url

If my url will like this:
http://id.factor.ua/bez-rubriki/checkout/?price=...
And url will contains bez-rubriki
How I can redirect using .htaccess to this url BUT without this slug, like next URL:
http://id.factor.ua/checkout/?price=...
As simple as this for an internal rewrite:
RewriteEngine on
RewriteRule ^bez-rubriki/checkout/$ /checkout/ [L,QSA]
For an external redirection add the R flag:
RewriteEngine on
RewriteRule ^bez-rubriki/checkout/$ /checkout/ [R=301,L,QSA]
The above are the versions to be used inside .htaccess style files.
A general note: if you have control over the http server configuration, then you should always prefer to place such rules inside the host configuration instead of using .htaccess style files. Those files are notoriously error prone, make things complex, are hard to debug and really slow the server down. They should only be used if there is no control of the http server configuration or if some app requires dynamic changes to the configuration.
So in case you want to place those rules in the host configuration you need a small modification. You have to include the leading slash (/) into the regex testing the request path:
RewriteEngine on
RewriteRule ^/bez-rubriki/checkout/$ /checkout/ [L,QSA]
And the version doing an external redirection:
RewriteEngine on
RewriteRule ^/bez-rubriki/checkout/$ /checkout/ [R=301,L,QSA]

How do I do a .htaccess rewrite that masks the forwarded URL?

I have a url that is www.blahblah.com/something
That is a remote service, I don't have anything to do with it.
How can I use .htaccess on my own server and rewrite from www.myurl.com so that the content displayed is all www.blahblah.com/something, but the address bar still reads www.myurl.com
No, this is not possible with foreign urls.
You can, however, do this locally. For example, look at this htaccess file:
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteRule ^some/test/url$ index.php?some=test&or=url [L]
In this scenario, if you visit www.myurl.com/some/test/url it will show as such on the browser, but your server will actually be running index.php in your document root with the parameters some=test&or=url.
This is only possible for scripts running on your server. You cannot do this on another server/domain. If you try this (eg, by changing index.php?some=test&or=url in the example above to http://www.blahblah.com/something), then apache will just redirect the browser to that url.
htaccess (Apache) makes the connection to the user, and the user is expecting a response from YOUR server. If you try to load content from another server, Apache would have to make that connection, load the resulting HTML or whatever, and pass it back to you. But this gets messy, especially when you get into cookies, SSL, javascript, etc.
My question is: why do you actually need this? I'm not sure I understand why it is a problem if the user's url changes. If it's a service you have no control over, why is it so bad to just send them to it?
You might want to research more about cache servers, or using PHP to to make the http call to the server you want and "pass through" the content, assuming you know beyond a doubt there will be no issues with cookies or SSL or whatever. But again, why not just send them to the proper URL?
Try this:
RewriteCond %{HTTP_HOST} ^DomainA.com
RewriteRule ^(.*) http://DomainB.com/$1 [P]
It works for me.
Source: http://www.inmotionhosting.com/support/website/htaccess/redirect-without-changing-url
mod_rewrite is the right way.
Make sure it is mod_rewrite is activated in our apache conifiguration.
add to the .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.blahblah\.com$ [NC]
RewriteRule ^(.*)$ http://www.myurl.com/$1 [R=301,L]
RewriteCond defines the condition. In this case if the http_Host is www.blahblah.com
RewriteRule defines what to do. In this case forward to your target domain. $1 is the rest of your URL
More Details you can find here: http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Resources