Hello I have been stuck with htaccess for the past 3 days and can not seem to figure out why my htaccess file is not changing my url correctly.
So I am attempting to turn this query string url from www.example.com/test/user-profile.php?id=4 into www.example.com/test/user-profile/4
the /test/ in the url is not a file, its a folder in the main public_html directory.
Here is my htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ /test/user-profile.php?id=$1 [L]
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^(.*)$ /test/user-profile/%1? [R=301,L]
As of right now it is going to the url www.example.com/test/user-profile/4 but it is not loading the entire page. Just beyond stuck, thank you for your time
With your shown samples, attempts; please try following htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
##RewriteBase /test/
##Extrenal redirect rule.
RewriteCond %{THE_REQUEST} \s/(test/user-profile)\.php\?id=(\d+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
##Internal rewrite rule.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/(.*)/?$ $1/$2.php?id=$3 [QSA,L]
JS/CS rewrite/redirect:
You may need to use base tag to fix your js and other relative resources. If you are linking js files using a relative path then the file will obviously get a 404 because its looking for URL path. for example if the URL path is /file/ instead of file.html then your relative resources are loading from /file/ which is not a directory but rewritten html file. To fix this make your links absolute or use base tag. In the header of your webpage add this <base href="/"> so that your relative links can load from the correct location.
Related
I have htaccess file where I am turning regurlar url example.com/result?from=Brampton&to=Calgary&submit= to Rides-From-Toronto-Calgary-submit seo friendly url. But problem is that I manually have to type url to visit seo friendly version of url, how can I achieve redirect using .htaccess ?
This is what I have so far
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^from [NC]
RewriteRule ^Rides-From-([^/]*)-([^/]*)-([^/]*)$ result?from=$1&to=$2&submit=$3 [QSA,NC,L,R=301]
any help is appreciated.
With your shown samples and attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
Also these Rules consider that your internal rewrite is happening to index.php file in case its something else file then please change its name to it in rules.
RewriteEngine ON
##External redirect rules.
RewriteCond %{THE_REQUEST} \s/result\.php\?from=([^&]*)&to=([^&]*)&submit=(\S+)\s [NC]
RewriteRule ^ /Rides-From-%1-%2-%3=%3? [R=301,L]
##Internal rewrite rules from here...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:.*?-){2}([^-]*)-([^-]*)-([^-]*)=(.+)/?$ result.php?from=$1&to=$2&$3=$3 [QSA,L]
Once my friend help me to solve this issue, I have written code like that. Where example.com/blog-detail?slug=slugvalue it goes to blog-detail.php and this url example.com/project?slug=test goes to project.php.
RewriteEngine ON
##Checking condition and getting matches in variables to be used while redirect in next rule.
RewriteCond %{THE_REQUEST} \s([^?]*)\?slug=(\S+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*)/(.*)/?$ blog-detail.php?slug=$1 [QSA,L]
I think it's work for only blog-detail page now i face the issue with this url
https://www.example.com/project?slug=test, it redirect me to 404 page, could you help me here.
With your shown samples, attempts; please try following htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
I have added 2 solutions here, so either use 1st rules set OR use 2nd rules set ONLY ONE at a time please.
1st solution: With 2 different rules set for each php file please try following rules.
RewriteEngine ON
###Rules for blog-detail.php redirect and rewrite.
##Checking condition and getting matches in variables to be used while redirect in next rule.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s(blog-detail)\?slug=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*)/(.*)/?$ blog-detail.php?slug=$1 [QSA,L]
###Rules for project.php redirect and rewrite.
##Checking condition and getting matches in variables to be used while redirect in next rule.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s(project)\?slug=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?:[^/]*)/(.*)/?$ project.php?slug=$1 [QSA,L]
2nd solution: A Generic solution with capturing groups to deal with both kind of php files together.
RewriteEngine ON
###Rules for blog-detail.php OR project.php redirect and rewrite.
##Checking condition and getting matches in variables to be used while redirect in next rule.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s([^?]*)\?slug=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(.*)/?$ /$1.php?slug=$2 [QSA,L]
NOTE1: Please keep both your php files(project.php and blog-detail.php) should be kept along with your htaccess rules file.
NOTE2: For JS/CS rewrite/redirect:
You may need to use base tag to fix your js and other relative resources. If you are linking js files using a relative path then the file will obviously get a 404 because its looking for URL path. for example if the URL path is /file/ instead of file.html then your relative resources are loading from /file/ which is not a directory but rewritten html file. To fix this make your links absolute or use base tag. In the header of your webpage add this <base href="/"> so that your relative links can load from the correct location.
I'm trying to redirect two different url to the same file.
The url are: http://localhost/CLIENTI/HOTEL/cp/amministratori and http://localhost/CLIENTI/HOTEL/cp/amministratori/test
Actually i currently see the first url, but when I open the second one i see a lot of error of files inclusion on client side (es js and css files on header/footer).
This is my .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^dashboard$ dashboard.php [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^amministratori$ dashboard.php [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^amministratori/test$ dashboard.php [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
that was all my htaccess code. The errors are 404 of bootstrap.js and main.js which contains custom code for the frontend. I've understood the problem but I've haven't undersood if there is a solution alternative to the leading slash, because my header and footer are included in php, so in /cp/amministratori they work as now, but obviously if I change with a leading slash they will not work anymore
With your shown attempts, please try following htaccess Rules. Make sure to place your htaccess file along with CLIENTI folder(not inside it).
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
RewriteBase /CLIENTI/HOTEL/cp/amministratori/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^dashboard/?$ dashboard.php [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?$ dashboard.php [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test/?$ dashboard.php [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [QSA,L]
JS/CS rewrite/redirect:
You may need to use base tag to fix your js and other relative resources. If you are linking js files using a relative path then the file will obviously get a 404 because its looking for URL path. for example if the URL path is /file/ instead of file.html then your relative resources are loading from /file/ which is not a directory but rewritten html file. To fix this make your links absolute or use base tag. In the header of your webpage add this <base href="/"> so that your relative links can load from the correct location.
I need the URL example.com/app to show the content from example.com/app/dist
The following code does exactly this.
RewriteEngine On
RewriteRule ^$ /app/dist/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/dist/
RewriteRule ^(.*)$ /app/dist/$1
BUT I also need the URL example.com/staging to show the content from example.com/staging/dist
With the current setup, the url example.com/staging is showing the content from example.com/app/dist
The .htaccess while is located in the /app and /staging folder (but it has to be the same file)
In the htaccess file in your staging folder, change all instances of app to staging
Edit:
You can try using relative paths instead of hardcoding the app or staging, but Ive had issues in the past, especially if there are other rules in play.
RewriteEngine On
RewriteRule ^dist - [NC,L]
RewriteRule ^$ dist/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ dist/$1 [L]
The idea is to remove the leading / from rewrites so a relative path is used. So technically it wont matter whatever folder the htaccess file is in
With the following .htaccess file, I obviously, get alot of 404's on loading scripts and css because the browser keeps looking in the wrong directory.
I am a newb at htaccess and have no clue about how to fix it.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^$ client/app/index\.html [L]
</IfModule>
Dir structure is as follows:
project /client / app / index.html
.htaccess is located in project directory.
I recomend you that redirect users with a 301 to correct path. This will end with your problems.
RewriteRule ^$ client/app/index\.html [L, 301]
If you are worried about seo look at https://support.google.com/webmasters/answer/93633?hl=en
All of the calls at your server are being redirected to your RewriteRule. Add a RewriteCond (RewriteCondition) to ignore CSS and JS files, if they exist:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^$ client/app/index\.html [L]
Explanation:
RewriteCond %{REQUEST_FILENAME} !-f
If the requested filename exists (css/main.css), do not go through with the RewriteRules.
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
If the file does not exist, but matches one of the extensions, do nothing. This is helpful because you get a 404 if css/main-2.css does not exist, rather than redirecting you to index.php.
And then finally is your own rule, which redirects all non-conditioned rules.
In your .htaccess file, this is what worked for me.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !=/index.html
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^ /index.html [R=301]
Thank you!