.htaccess how to create an URLs like http://example.com/~someuser - .htaccess

How do I can create and process an URLs like this via .htaccess rules: http://example.com/~someuser
And then get a parameter "username" as a part of $_GET array. Thanks in advance.

Something like this:
RewriteEngine On
RewriteRule ^~([^/]+)/?$ /script.php?user=$1 [L]
If those rules are placed in the htaccess file in your document root, then it'll capture the "someuser" part of the URL and rewrite it to the script.php script and set it as the $_GET['user'] variable. You can replace script.php with whatever name of the script you want the variable to go to.

Related

.htaccess regex redirect avoid by file type

I have two URL conditions and I wanted to redirect them like this:
https://www.example.com/feeds/4aceXy to https://www.example.com/direct_feed/4aceXy
Now the problem is, I am also using the URL for an older link like this one:
https://wwww.example.com/feeds/5bdb39711b41d479273e678a6f356603d7109ffc.xml
I wanted to avoid redirect with .xml extension here is my current redirect:
RewriteRule feeds/(.*)?$ https://wwww.example.com/direct_feed/$1 [QSA,L]
It works fine but I don't want to redirect it with .xml based URL.
My question is - is there a condition that can help me to avoid the rewrite if a parameter contains .xml in regX (.*)?$
You can use a negative lookbehind:
RewriteRule feeds/(.*)?(?<!\.xml)$ https://wwww.example.com/direct_feed/$1 [QSA,L]

Is it possible to remove a "folder" at the end of a URL using an .htaccess file?

so I have a bunch of URLs
http://foo.com/people/smith/john/1
http://foo.com/people/cartman/eric/2
http://foo.com/people/simpson/bart/3
I want to change the URLs like so
http://foo.com/people/smith/john/
http://foo.com/people/cartman/eric/
http://foo.com/people/simpson/bart/
So basically, I think I need to write a condition that looks for the folder 'people' and removes the third directory from the URL. Is this possible with my .htaccess?
In the htaccess file in your document root, try:
RewriteEngine On
RewriteRule ^people/([^/]+)/([^/]+)/.+ /people/$1/$2 [L,R=301]
This redirects the browser (changing the URL in the address bar) from
http://foo.com/people/smith/john/1
to
http://foo.com/people/smith/john/
If you don't want the URL in the browser's address bar to change, remove the ,R=301 from the square brackets.

want to replace .index.php?backLinks_free_Backlinks=1&totalRows_free_Backlinks=7 part by /backLinks_free_Backlinks/1 from my url using .htaccess

i have a link look like this
http://backlinks.cheapratedomain.com/index.php?backLinks_free_Backlinks=1&totalRows_free_Backlinks=7
want to replace .index.php?backLinks_free_Backlinks=1&totalRows_free_Backlinks=7 this part by /backLinks_free_Backlinks/1 using .htaccess
after replacing link will be look like this
http://backlinks.cheapratedomain.com/backLinks_free_Backlinks/1
Try adding this to your htaccess file in the document root:
RewriteEngine On
RewriteRule ^backLinks_free_Backlinks/([0-9]+)$ /index.php?backLinks_free_Backlinks=$1&totalRows_free_Backlinks=7 [L,QSA]
You can remove the &totalRows_free_Backlinks=7 bit from the target if you don't need it.

.htaccess 301 Redirect Appending Query's to End of URL

I am hoping someone can help with an unusual situation.
I have one main rewrite rule in place in my httpd.conf file which handles all of our dynamic content. The rule looks like this and works fine:
RewriteRule ^(.)(/./d/[^.]*)$ /category/refine.cgi\?\&a\=$2
The problem I have is that when I try to use .htaccess to create a simple 301 redirect, the query parameters are automatically appended to the end of the URL's so the final result looks like this:
http://www.example.com/category/page.html?&a=/category/subcategory/something/d/page/
Notice that the query string is appended to the URL when using .htaccess to create a 301 redirect.
I have solution for this on a case-by-case basis, but it's not practical to create a new rule each time I want to do a simple 301 redirect.
So, I am wondering if I can edit my "main rule" in any way so that when .htaccess is used to create redirects, the query parameters are not appended to the target URL.
Thanks in advance for any help you can provide.
If you have multiple simple redirects for which you want to suppress query string values you could put all the redirects in a RewriteMap (since you already have access to httpd.conf), and have one .htaccess rule that suppresses the query strings as below
place in htaccess
#if there is a match in the map
RewriteCond ${redirect_map:$1} !=""
RewriteRule ^(.*)$ ${redirect_map:$1}? [R,L]
place in httpd.conf
RewriteEngine On
RewriteMap redirect_map txt:/usr/local/apache/conf/redirect.map
contents of /usr/local/apache/conf/redirect.map
key followed by a space followed by target
directory/subdirectory1/subdirectory2/ example/category7/subdirectory/file.html
directory4/subdirectory2/subdirectory9/ example/category5/subdirectory4/file332.html
That's what your rule has defined it to do:
RewriteRule ^(.)(/./d/[^.]*)$ /category/refine.cgi\?\&a\=$2
It says to create a URL that will look like:
category/refine.cgi?&a=/foo/bar/
If you don't want that to happen, change your rule to be:
RewriteRule ^(.)(/./d/[^.]*)$ /category/refine.cgi\?

.htaccess modification

I am using direct paths for downloading files from my site. the link is something like this
http://www.site.com/download.php?dir1/dir/dir3/file.doc
i want to wrap it with mod rewite rules so that only below link should be appeared
http://www.site.com/download
file, dir and dir3 are variable.
what i'hv to do in my .htaccess file?? Any Idea??
A simple redirect would be:
RewriteRule ^http://www.site.com/download/(.*)/?$ http://www.site.com/download.php?dir1/dir/dir3/$1 [NC,L]
This will take any request for something in the 'artificial' download directory and route it to the real location.
You can add more complex rules stripping out filetypes etc depending on your needs, or redirecting a 'name' to a filename etc etc..
e.g:
RewriteRule ^http://www.site.com/download/pdf/(.*)/?$ http://www.site.com/download.php?dir1/dir/dir3/$1.pdf [L,NC]
This would have an artificial PDF folder containing a filename ex the extension, routing to a .pdf doc....you can shape the redirect any way you like really...depends on the format you prefer
Not specific question. What is dir1/dir/dir3/file.doc means? If you want to get http://www.site.com/download.php?dir1/dir/dir3/file.doc, when you go to http://www.site.com/download do the next things in your .htaccess file.
RewriteEngine on
RewriteRule ^download/(.*)/?$ download.php?dir1/dir/dir3/file.doc [L]

Resources