When using the following entries in my .htaccess file
RewriteEngine On
SetEnv rewritten 0
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)(/|$) /#$1 [L,NE,R=302,E=rewritten:1]
Which rewrites <url>/test/ to <url>/#test.
The goal is to keep the "test" part in an environment variable, so I can use php to change the Facebook metatags and also use the anchor to scroll to a section on my one-page-site. For the above example, the rewritten variable is always 0 and if I remove the SetEnv line, the variable will not be set at all.
I have read that the variable might not be retained after a redirect which the 302 will do. Is there any other way to do this? Other solutions are welcome - my overall goal is to link to a specific part of my application and set the metatags according to this section.
There are multiple questions here on SO where people try to rewrite an url with the anchor part, to an url without it - this is not my question, since it can't be done.
Thanks.
Edit
The above is not possible. Added the following to .htaccess, which redirect <url>/test to <url>/?test#test. PHP can then check if the test param is set and modify meta-tags accordingly.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)(/|$) /?$1#$1 [L,NE,R=302]
The above is not possible. Added the following to .htaccess, which redirect <url>/test to <url>/?test#test. PHP can then check if the test param is set and modify meta-tags accordingly.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)(/|$) /?$1#$1 [L,NE,R=302]
Related
I ned to change this url
https://halopredictions.com/blog/index.php?url=german-bundesliga-prepare-to-return-on-9-may
to
https://halopredictions.com/blog/german-bundesliga-prepare-to-return-on-9-may
This is my .htaccess that am currently using
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ index.php?url=$1 [NC,L]
I have already enabled mod rewrite
The htaccess is on this path ...\blog\.htaccess
Any help I will highly appreciate
Before proceeding with the answer, if I understand correctly you want to redirect from https://halopredictions.com/blog/german-bundesliga-prepare-to-return-on-9-may To https://halopredictions.com/blog/index.php?url=german-bundesliga-prepare-to-return-on-9-may.
Since SEO Urls are used by the visitors, we have to convert them to actual path.
The below RewriteRule will be sufficient to achieve the above condition.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/([A-Za-z0-9-\+]+)/?$ blog/index.php?url=$1 [NC,L,QSA]
Explanation:
^blog/([A-Za-z0-9-\+]+)/?$ - This part is what you want to rewrite from. Since, your URLs will be like in the pattern https://halopredictions.com/blog/german-bundesliga-prepare-to-return-on-9-may, we need to get the url part after blog/.
I have used a regular expression ([A-Za-z0-9-\+]+)/? to match the url part and it can be referrenced using $1.
Now we have to use $1 in our new url. For that, we have written the second part of RewriteRule where you can assign the referrence.
blog/index.php?url=$1 - Now, as you would assume we are using the $1 reference after index.php?url=, so that it will append it to the URL Query param and should lead to a valid path.
By the way, ^ this is used to indicate the start and $ for the end.
I have the following url:
http://example.com/files/101-info/index.php
My questions is:
Is it possible to remove the 101- in the url?
Is it possible to rewrite the name in the address bar, but keep the file in the current location?
I would like to make the file appear to be at:
http://example.com/files/info/index.php
but actually remain in:
/var/www/htdocs/files/101-info/index.php
Here's what I tried, but I couldn't remove the 101- and the redirect didn't work:
RewriteEngine On
RewriteRule ^([^/]+)/([0-9]+)-([^\/]+)/(.+)\.php$ /$1/$2/$4 [R] // also tried [NC,QSA]
NOTE: Keep in mind if you have other rules in your .htaccess that you have not mentioned in your question and you don't place the below rules at the proper place, it will not work as expected.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+files/[0-9]+-([^/]+)/([^\s\?]+) [NC]
RewriteRule ^ /files/%1/%2 [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(files)/([^/]+)/([^/]+)$ /$1/101-$2/$3 [L]
First rule will remove the 101-, the 2nd rule will accept the 101- internally without having to change the folder name or the place your files are.
Change it from [R=302,L] to [R=301,L] once you confirm it is working.
Since you were previously using a redirect, your browser might be cached, so clear you cache and make sure to test the URL with a different browser until the cache of yours is cleared as it doesn't always happen after deleting it.
So I have my framework that handle all URLs via one index.php page. If I put site.com/profile/user it will go to site.com/index.php?url=profile&next=user and parameter "url" loads different pages for example /home would be index.php?url=home
Problem now is that I wont to expand my rewrite rules in htaccess file so I have one more additional parameter which would be for example site.com/profile/user/auctions and it would look like site.com/index.php?url=profile&next=user&category=auctions.
My rewrite rules are listed below:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^]+)/([^/]+)(/([^/]+))?$ index.php?url=$1&next=$2
So problem is I can't put additional "category" parameter which would be index.php?url=$1&next=$2&category=$3 , whenever I change rules one parameter works but other dont.
Thank you for reading, and I hope it could be solved.
Try this one:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?url=$1&next=$2&category=$3 [L]
I take this rule from my own .htaccess file and just rename variables, so it must work.
If the reason just in this file of course
I currently have a .htaccess file that allows people to enter the URL without the php extension, such that http://domain.com/account redirects to account.php
I would like to be able to have it so that if I enter http://domain.com/account/contactinfo (or http://domain.com/account/settings/groups and so on) it still goes to account.php, but I am not sure how to change what I have to achieve this.
Current .htaccess :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(([A-Za-z0-9\-_\.]+/)*[A-Za-z0-9\-_\.]+)?$ $1.php
Any help appreciated! Obviously if there exists a folder it should follow that path (e.g. if /folder/page.php exists, then http://domain.com/folder/page/create would go to folder/page.php)
Try this is you don't need to pass any URI info into query string (i.e. your app will still look at $_SERVER['REQUEST_URI'])
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z0-9\-_\.]+)(/[A-Za-z0-9\-_\.]*)?$ $1.php&q=$2 [QSA]
# Note the optional '&q=$2' on line above if you want to make removed part of URI available as passed parameter
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ / [L,QSA]
Note that since I removed the condition to check for a valid php file, I added a second conditional rewrite rule to just redirect to site root if the re-written request does not point to a valid PHP file. You could obviously redirect this to a 404 page or whatever else you might want to redirect to. Or you could remove this altogether and let Apache give it's default 404 response.
I have set up my .htaccess to redirect calls to mydomain.com/something.html to mydomain.com/index.php?q=something
This works fine.... but I noticed that since moving to a new hosting I am getting multiple queries for the query "missing".
I am pretty sure it is due to the default html page for wrong URLs being "missing.html" which is redirected as mydomain.com/index.php?q=missing. So any missing URL will cause my PHP script to be run with "missing" as input.
Is there a way to keep the URL redirect and manage the calls to missing.html without actually calling my PHP script for missing URLs?
Edit with solution; here is my .htaccess which does the redirect only if there is no existing URL:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)\.html index.php?q=$1
Seems the RewriteCond need to be placed before (as shown above).
You're looking for RewriteCond.
Try this in your .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
These rules will also stop rewriting for anything is an accessible path. If you want strictly missing.html, then the following rule will be sufficient:
RewriteCond %{REQUEST_URI} !/missing.html
Note these directives must come before RewriteRule.
Isn't is possible to add a rewriting rule which takes precedence over the other one and specifically rewrites mydomain.com/missing.html to the location of your 404 page? (Seing the redirection rule you already set up would help.)