I have an external backlink which is linking incorrectly to my website.
They are adding /%E2%80%8E to the end of the link so it is coming in as http://mywebsite.com/%E2%80%8E.
I want to use htaccess to redirect these people to my homepage.
This is what I currently have:
#This version does not work for some reason
RewriteRule %E2%80%8E https://mysite.com [B,R,L]
RewriteCond %{QUERY_STRING} %E2%80%8E
RewriteRule .? https://mysite.com [B,R,L]
# This version works if I type in the DECODED version of the string
RewriteRule ‎ https://mysite.com [R,L]
RewriteCond %{QUERY_STRING} ‎
RewriteRule .? https://mysite.com [R,L]
Thanks
If you don't want to use the decoded string, you can use \x##. The reason why the decoded string works is that in RewriteRule's, the URI is decoded before the pattern is applied.
RewriteRule ^\xE2\x80\x8E$ / [L,R=301]
Give this a try in your htaccess.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/%E2%80%8E\s [NC]
RewriteRule ^ https://mysite.com/ [L,R=301]
You could solve this problem without using .htaccess rewrite. On some of my website I do check, either in the header of the page (with PHP or JS) or in a custom 404 page.
In my opinion this method is slightly better than mod rewrite just because it doesn't require you to have the mod_rewrite module enabled on the server.
Related
I have an old php dynamic web site that uses index.php to generate everything. I changed the index.php file to product.php and made keyword rich url using product.php. The keyword riche works. Now i need to make a 301 redirect in the htaccess so that the indexed links will be redirect to product.php
This works
# Rewrite keyword-rich URLs
RewriteRule ^product/(.*)/.*-([0-9]+)\.html$ product.php?lang=$1&cat=$2 [L]
# Rewrite keyword-rich URLs
RewriteRule ^product/(.*)/.*-([0-9]+)/.*-sc([0-9]+)\.html$ product.php?lang=$1&cat=$2&subCat=$3 [L]
but i tried to add before these rules this but it is not changing the url
RewriteCond %{THE_REQUEST} ^index\.php?(.*)$ [NC]
RewriteRule ^product.php?$1 [R=301,L]
I am using mamp to do my testing
Try this instead (before your existing rules):
RewriteCond %{THE_REQUEST} \ /+index\.php\?(.*)$ [NC]
RewriteRule ^ product.php [R=301,L]
Note the \ before the ? in the condition, then the space between the ^ and product.php. The query string will automatically get appended. You don't need to backreference it.
I recently developed a web site using php which accepts a id as its input via get.
http://website.com/profile.php?id=123. its the old fashion way, I've seen sites use pretty url's like http://website.com/username.html
So in order to archive this, i managed to do some research and end up with this code blow.
Also i'm passing the username in the query string.
http://website.com/profile.php?id=123&username=test_user
the parameter ID is only used as a input for the profile.php
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /profile.php [NC]
RewriteCond %{QUERY_STRING} ^id=([^&]+)&username=([^&]+) [NC]
RewriteRule ^.* http://%{HTTP_HOST}/%2.html [R=301,L]
The final output i seek is http://website.com/username.html
Is their anything wrong in this ? this doesn't seem to work. Am i doing something wrong ? if so please be kind to let me know where my error lies. Thank you.
Try this rule:
RewriteEngine On
RewriteRule ^profile/([^/\.]+)/([^/\.]+).html/?$ profile.php?id=$1&username=$2 [L]
then you should be able to use
http://website.com/profile/123/username.html
as the URL. You can ignore the profile part but I don't recomment it because it would clash with other URLs of your website.
If you only need the username, use
RewriteRule ^profile/([^/\.]+).html/?$ profile.php?username=$2 [L]
and the URL would be
http://website.com/profile/username.html
You can use this rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} /profile.php\?id=([^&]+)&username=([^&]+) [NC]
RewriteRule ^ /%1/%2.html? [R=301,L]
RewriteRule ^([0-9]+)/([^/.]+)\.html$ profile.php?id=$1&username=$2 [L,QSA,NC]
I've migrate a dynamic .asp site to static site by using sitesucker osx app...
everything works done if you access directly!
old url:
http://www.mysite.com/content.asp?L=1&IdMen=158
new url:
http://www.mysite.com/content.asp-L=1&IdMen=158.html
i get page not found from my old referral inbound links (google, yahoo etc...)
i would like, by using .htaccess, redirect all links that not contains ".html" to permanent redirect too html page...
i've tried this but not work:
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.html
RewriteRule ^(.*)$ $1.html [R=301,L]
many thanks
Frankie
As per examples if you want to modify query string then you need this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} !\.html$ [NC]
RewriteRule !\.html$ %{REQUEST_URI}-%{QUERY_STRING}.html? [L,R=301,NE]
I have a slight problem.
I have a site that uses these formats for viewing certain PHP documents:
domainname.com/server.php?id=14
domainname.com/changeserver.php?id=14
domainname.com/customize.php?id=14
I would like to make these:
domainname.com/server/14
domainname.com/changeserver/14
domainname.com/customize/14
I also have various URLs such as /index.php and /login.php I would like to clean up.
If someone were to type the original URL, such as /server.php?id=14, I would like it to redirect to it's clean URL.
If anyone can provide me with a config or help me along the way to making one that can do this, it would be greatly appreciated.
To prevent an infinite loop while externally redirecting the ugly url to the clean url and internally rewriting it back to the ugly url, you can use the THE_REQUEST trick.
#External redirect
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /(server|changeserver|customize)\.php\?id=([^&]+)\ HTTP
RewriteRule ^ /%2/%3? [R,L]
#Change above to [R=301,L] once all rules work as expected
#Internal rewrite
RewriteRule ^(server|changeserver|customize)/(.*)$ /$1?id=$2 [L]
You need to rewrite your URLs in .htaccess file.
RewriteEngine On
RewriteRule ^server/(.*)$ server.php?id=$1 [R=301,L]
here is a good guide on URL rewriting
http://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
In this case you have to use a 301 Redirect of URLS. Here is an example:
Redirect
www.example.com/a1/articles.php?id=1&name=abc
to
www.example.com/article/1/abc
For this you have to modify the .htaccess file with the following:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=(.*)&name=(.*)$
RewriteRule ^articles\.php$ /article/%1/%2? [R=301]
RewriteRule ^article/([^-]+)/([^-]+)$ /articles.php?article_id=$1&article_name=$2 [L]
RewriteEngine On
RewriteRule ^server/([A-Za-z0-9-_,()'+]+)?$ server.php?id=$1
RewriteRule ^changeserver/([A-Za-z0-9-_,()'+]+)?$ changeserver.php?id=$1
RewriteRule ^customize/([A-Za-z0-9-_,()'+]+)?$ customize.php?id=$1
To adapt to your code. Here is the answer.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^server\.php$ /server/%1? [R=301]
RewriteRule ^server/([^-]+)$ /server.php?id=$1 [L]
Check the Above code. I think this will help you.
I'm trying to rewrite a url but I can't seem to make it happen.
I just want one working example so I can move on to all pages of my website.
So I have this link:
www.domain.com/article.php?id=1
And I can to change it to:
www.domain.com/article/1/
That's ok for now, later I'll replace the number with the article title.
This is what I have on my .htaccess file:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^article\.php\?id=([0-9]+)$ article/$1/
What is wrong it it? I heard that the .htaccess file needs to be in ASCII if using FTP to save in on the server but I don't know how to do it since I created it by myself.
You can't match against the query string in a rewrite rule, you need to match against %{QUERY_STRING} in a condition:
RewriteEngine On # Turn on the rewriting engine
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^article\.php$ article/%1/? [L]
This internally rewrites your request to /article/1/. The browser will still see the old URL.
What you are more likely looking for is to match against the request:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+article\.php\?id=([0-9]+)
RewriteRule ^ /article/%1/? [L,R=301]
RewriteRule ^article/([0-9]+)/?$ /article.php?id=$1 [L,QSA]
You are doing this backwards, quite literally.
RewriteRule ^article/([0-9]+)$ /article.php?id=$1