Mod_Rewrite variable dynamic directory inside of two static ones - javascript

So I am trying to force javascript files to reload whenever we as developers update the files. In order to do this we are using the answer found here https://stackoverflow.com/a/7671705/805779 and we think this will work great however I am not sure how to do the mod rewrite. So here is the link I have
www.mysite.com/js/v1/myjs.js
I need to change it to
www.nysite.com/js/myjs.js
This is what I have so far but it is not working
<IfModule mod_rewrite>
RewriteEngine On
RewriteBase /
RewriteRule ^/js/(.*)$/(.*)$ js/$2 [L,NC,R]
</IfModule>
After some more testing I found my main issue was not adding .c to my IfModule, with that in mind the following is "partially" working
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/css/(.*)/(.*)$ /css/$2 [L,NC,R]
RewriteRule ^/js/(.*)/(.*)$ /js/$2 [L,NC,R]
</IfModule>
This works for my classic example
www.mysite.com/js/v1/myjs.js => www.nysite.com/js/myjs.js
However this does not work if more then one variable are involved. aka
www.mysite.com/js/v1/min/pages/myjs.js
My version number v1 will always be directly after the root folder so js/v1 or css/v1 and I want to remove the v1 and keep everything after it

You can use this single rule for this redirect:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^/?((?:css|js))/v\d+/(.+)$ /$1/$2 [L,NC,R]
</IfModule>

Related

How do you do dynamic url. example.com/profile/CroatiaGM

I am doing the user control panel, and I have some problems I am not sure how to do this dynamic profile link currently. example.com/profile/CroatiaGM What would be the best way to go about this, My site is mainly PHP, is it better to use js or?
I'm assuming you're looking to rewrite a URL, where you'd like to rewrite example.com/profile.php?user=CroatiaGM to example.com/profile/CroatiaGM.
in such an instance I believe this would be helpful if you don't already have a .htaccess file, I suggest you create one in your main directory/public_html
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^profile/([^/]+)?$ profile.php?username=$1
RewriteRule ^profile/([^/]+)/([^/]+)?$ profile.php?username=$1&type=$2
</IfModule>
or
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)?$ $1.php?username=$2
RewriteRule ^([^/]+)/([^/]+)/([^/]+)?$ $1.php?username=$2&type=$3
</IfModule>
Here's a working example.
https://locationtestproject.000webhostapp.com/test/myname
I strongly advise making a back of the .htaccess before saving any changes.
https://help.dreamhost.com/hc/en-us/articles/215747748-How-can-I-redirect-and-rewrite-my-URLs-with-an-htaccess-file-
How to use .htaccess for beautiful links
https://docs.bolt.cm/4.0/howto/making-sure-htaccess-works
https://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/

changed .htaccess and permalink now everything besides homepage 404s

I added this code to my .htaccess file to get rid of index.php, but now all the pages 404 (the hyperlinks are mapped to the correct url)
# 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
i set up my permalinks using http://1.1.1.1/%postname%/
what are the next steps in order to troubleshoot this issue?
can anyone provide me with a rewrite link and where that file may be located (I don't fully understand the other similar posts as they all wrote their own rewrite rules, where would I find that file?)
I have already installed monkeyman rewrite analyzer, but don't know where to start troubleshooting from there.
Any links or information is helpful, even if is not a direct solution to the problem, I will post my solution when completed.

Laravel - href in ignored subfolder trouble

I have a bit of trouble with in a folder that laravel is ignoring.
In my .htacces I have a line that will ignore my folder called 'doc'.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteRule ^(doc) - [L] <-- this one
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
and it works like it should.... kinda.. Example: 'localhost/doc/main.js' works fine if i type it direct into my webbrowser.
Here is my problem: in my /doc i have a 'index.html' that include/require/link some files into it from the doc dir. So in the index.html I have a line
<script data-main="./main.js" src="./vendor/require.min.js"></script>
When I go to 'localhost/doc' I get error that file cant be found (and alot others). linking to 'localhost/vendor/require.min.js'. This should be 'localhost/doc/vendor/require.min.js'
NOTE: this file is generated with ApiDocJS
Anyone have a solution for me, am stuck for about 4 hours now... am getting frustrated
I'll suggest to try to do the same in a different way.
If you're stuck for so long on a problem like that, try changing the way you are following to accomplish your goal.
Why that directory?
Why ignoring it in .htaccess?
Why not separate the logic of the app from the thing I want to hide?
Can I configure the libraries I want to use in order to specify a directory?
Can I refactor some of my code to prevent the ignore-the-dir-in-htaccess thing? (Since it's not common to do that)
That questions will move you to another direction that may or may not help you with your problem, but that's what I always do on a problem I'm stuck for more than 30 minutes, asking myself for another way of doing the same thing.

Link not opening properly after hiding file extension using htaccess

I have used ht-access to hide .php file extension. Inside .htaccess file my code is :
# Apache Rewrite Rules
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
# End of Apache Rewrite Rules
</IfModule>
It's hiding .php file extension well. But problem is that, link inside href attribute is not opening properly.
I have two files:
public_html/afiliate/product_details.php
public_html/afiliate/afiliate_login.php
Inside product_details.php I have given link of afiliate_login.php like this
Log in
Clicking Log in I should go to a page like
example.com/afiliate/afiliate_login/
But it takes me to
example.com/afiliate/product_details/afiliate_login/
And hence a 404 error
Again notice that, if i manually type this URL
example.com/afiliate/afiliate_login/
that's OK, it goes to the page as expected.
Now how I can make links work properly?
After this code:
RewriteRule ^([^\.]+)/$ $1.php
put the below code
RewriteRule afiliate_login$ afiliate_login.php [L]
basically means that any request that comes to afiliate_login will show the data from afiliate_login.php
You got that problem because in your anchor tag you are using relative url. Since your url look like this without the file extension like, example.com/afiliate/afiliate_login/ , affiliate_login/ will be also considered as a directory and the html thinks that the anchor tag is in affiliate_login/ but it in reality it does not exists there so you get 404 error.
Previously the browser considered product_details.php as a file. but now its a folder, because its product_details/
one option available here is to update the link's like this
Log in
but its always suggested like the link below
Log in

How to hide .html extension in url using Javascript

Is there any way to hide .html extension from URL using JavaScript.
FOR ex:
mysite.html hide.html here from url
You cannot achieve this through javascript, this need to be done through .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
More information about mod_rewrite module here.
You can with html5, but also you can use url rewriting, or seo friendly urls, on the server side.
window.history.pushState({"html":'<html></html>',"pageTitle":'this is my title'},"", 'http://stackoverflow.com/mynewurl');
However keep in mind you can only change the url to one which is the same domain as the original!
There is a great library called history.js which helps with cross browser and version compatibility

Categories