Appending an extension automaticlly if url does not contains - .htaccess

I am using the following Rewrite rule
RewriteMap map txt:C:/seo/mapping.txt
RewriteCond %{QUERY_STRING} (?!admin)
RewriteCond ${map:$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^(.*?\.(?:html|gif|jpg|png)) ${map:$1} [NC,L,NS,QSA]
But there are some cases where the URL does not contains an extension
like user should be able to enter either /xyz.html or /xyzWhat I need is that if user don't enters an extension then in rewrite rule .html should be automatically appended.
Something like
RewriteRule ^(.*?\.(?:html|gif|jpg|png)) ${map:$1\.html} [NC,L,NS,QSA]

I would separate these two cases like this:
RewriteMap map txt:C:/seo/mapping.txt
RewriteCond %{QUERY_STRING} (?!admin)
RewriteCond ${map:$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^(.*?\.(?:html|gif|jpg|png)) ${map:$1} [NC,L,NS,QSA]
RewriteCond %{QUERY_STRING} (?!admin)
RewriteCond ${map:$1.html|NOT_FOUND} !NOT_FOUND
RewriteRule ^([^/.]+)$ ${map:$1.html} [NC,L,NS,QSA]

I came up with this, which will capture the filename, and if there is an extension it will capture that too. The filename capture group then has .html appended to it. However I have not tested this with multiple / in it, but I'm sure you can adapt it as necessary.
Example on Regexr
RewriteRule ^\/(\w+)[.]?(\w)? $1.html [NC,L,NS,QSA]

Related

URL rewrite like instagram profile

I'm trying to rewrite my URLs with unique slug like Instagram or Facebook.
Example: facebook.com/joe
My URLs are like that: website.com/user.php?username=joe
I try this rule in .htaccess:
RewriteEngine On
RewriteRule ^([a-z]+)$ user.php?username=$1 [L]
But it doesn't work, it redirects on /user.php.
It works if I use this rule:
RewriteEngine On
RewriteRule ^u/([^/]*)$ /user.php?username=$1 [L]
But result is website.com/u/joe and I prefer without u.
Any idea?
Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ user.php?username=$1 [NC,L]
The .* in the pattern ^(.*)$ matches /anything and the parentheses help capture the anything part as a $1 variable used in the substitution URL as user.php?username=$1.
In case if you need multiple parameters you can simply add &%{QUERY_STRING} after $1 to separate and add them to the end of your query string.
for example : if you pass website.com/joe?age=31 the result will be website.com/user.php?username=joe&age=31.
Finally, the flag NC simply makes the rule non case-sensitive, so it matches /Joe or /JOE as well.

Replace all occurrences of a word with another in .htaccess

I need to change one word which is there in all URLs linking to my site.
I tried with the following rule but it works fine only when the word is the first one in URL
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} ^/foo(?:/)?(.*)$
RewriteRule ^[/]?foo[/]?(.*) /bar/$1 [R=permanent,L,NE]
When I add wildcard entries to REQ_URI, output is not proper, I am not sure how to handle replacements. www.example.com/abc/def/foo/ghi is rewirtten as www.example.com/bar/abc/def
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteCond %{REQUEST_URI} ^(.*)/foo(?:/)?(.*)$
RewriteRule ^(.*)[/]?foo[/]?(.*) /bar/$1 [R=permanent,L,NE]
How do I correct the rewrite rule ?
RewriteRule ^(.*)/foo/(.*)$ $1/bar/$2 [R=301,L]
RewriteRule ^foo/(.*)$ bar/$1 [R=301,L]
This assumes foo is a folder name. Generally when setting something like this up you want to have some sort of delimiter (in this case the slashes) so that, for example, "food" doesn't become "bard".

Strip query_string

I want to get rid of some query string on my whole website (explicitly facebook shared/like query which usually begin with fb_action).
I thought about using .htaccess to do that.
I want this:
Link 1)
http://example.com/documents/de_desmarais_en_sirois?fb_action_ids=10151430962018298&fb_action_types=og.likes&fb_source=timeline_og&action_object_map={%2210151430962018298%22%3A157643874359578}&action_type_map={%2210151430962018298%22%3A%22og.likes%22}&action_ref_map=[]
to look like:
Link 2)
http://example.com/documents/de_desmarais_en_sirois
In my .htaccess, I added:
RewriteCond %{QUERY_STRING} fb_action
RewriteRule ^(.*) /$1? [R=301,L]
But, when I go on the original link, I am directed to my root folder example.com/pages.php, but I want to keep the first part of the URL which is example.com/documents/de_desmarais_en_sirois. I don't want to go to my root.
What should I modify/do?
I assume you've added those rules to the htaccess file in the /documents/ directory?
You'll need to remove the leading slash in your rule's target and add a rewrite base:
RewriteBase /documents/
RewriteCond %{QUERY_STRING} fb_action
RewriteRule ^(.*) $1? [R=301,L]
The query string match could be improved a bit though:
RewriteCond %{QUERY_STRING} (.*)(^|&)fb_[^&]+(.*)$
RewriteRule ^(.*) $1?%1%2 [R=301,L]
RewriteCond %{QUERY_STRING} (.*)(^|&)action_[^&]+(.*)$
RewriteRule ^(.*) $1?%1%2 [R=301,L]
To remove only the facebook query string parameters, if you have other parameters that you want to preserve.

Specific .htaccess redirect based on query_string

I need this logic to work:
I want rewrite this string for users to see
http://mysite.com/index.php?cl=mykeystring
to
http://mysite.com/otherkey/
http://mysite.com/index.php?myvar=test&cl=mykeystring&mysecondvar=morevalue
to
http://mysite.com/otherkey/myvar=test&mysecondvar=morevalue
But when http://mysite.com/otherkey/ is written, so load
http://mysite.com/index.php?cl=mykeystring, but no redirects will be done.
Is it possible? There are no possibility to change anything in codes, but only .htaccess
This logic is nearly realized by this code:
RewriteCond %{QUERY_STRING} ^(.*?)cl=mykeystring(.*?)$ [NC]
RewriteRule ^index.php$ /otherkey/%1%2? [R,L]
RewriteRule ^otherkey/(.*?)$ /index.php?cl=mykeystring&$1
but im getting some not needed amp symbols on first rewrite rule. any solutions?
I think you can do something like this:
RewriteCond %{QUERY_STRING} cl=mykeystring [NC]
RewriteRule ^index.php /otherkey/ [QSA]
Docs here: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

QUERY_STRING in .htaccess While redirecting

How can I allow visitors to use this link (www.example.com/news?id=34) in order to see (www.example.com/index.php?page=news&id=34)
Right now I am hiding the extensions of my PHP files in order to make the links look nice. When visitors go to (www.example.com/news) they can see the page (www.example.com/index.php?page=news). However, when they go to (www.example.com/news?id=12) they get a 404 error.
At this point, PHP does not recognize the id parameter. Here is what I currently have in my .htaccess file
Options +FollowSymlinks
RewriteEngine on
# catch request with no querystring like /Home
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^([^/]+)$ /index.php?page=$1 [L]
# make other requests with a non-empty query string go to /index.php?id=XXXXX
RewriteCond %{QUERY_STRING} ^.*$
RewriteRule ^$ /index.php?id=$1 [QSA,L]
Your second test pattern (^$) only matches the case where the user doesn't put any path information in, but does include a query string (e.g. www.example.com/?id=12). Also note that the backreference $1 has no value in that rule either.
The simplest solution is to just combine the two rules, since you can use the QSA flag to do the work of appending the id=# part for you:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /index.php?page=$1 [QSA,L]
The condition of your first rule fails as the query is not empty. Try it without that condition but with the following condition instead:
RewriteCond $1 !=index.php
RewriteRule ^([^/]+)$ /index.php?page=$1 [L]

Resources