mod_rewrite to 302 a blank query string - .htaccess

I'm currently working with:
RewriteEngine On
RewriteBase /somepath
RewriteRule ^/$ home [R]
I'd like to have requests of GET /somepath to redirect via 302 to /somepath/home. What am I missing here?

When using mod_rewrite in an .htaccess file, the base path is removed from the requested URL path before testing the patterns. That means /somepath (or rather /somepath/, since you’re actually in /somepath/) is removed so that the remaining path is empty. So:
RewriteRule ^$ home [R]

Related

Friendly url pagination

I am new on that htaccess thing.
My problem is, I have a pagination on index.php:
http://localhost/index.php?pagina=2
I want to access this with this url:
http://localhost/page/2
I try on htaccess:
RewriteEngine On
RewriteRule /page/(.*)$ index.php?pagina=$1
but it gave me a 404 error.
In per-directory .htaccess files the directory prefix, of where this .htaccess file is located, is removed from the URL-path when pattern matching. In the document root, this is simply / (a slash). So your RewriteRule would need to be rewritten as:
RewriteRule ^page/(.*) /index.php?pagina=$1 [L]
Use:
RewriteEngine On
RewriteRule page/(.*)$ index.php?pagina=$1
without first /

Trail slash with multiple htaccess

I have directory structure like this:
public_html
.htaccess[1]
-apps
-.htaccess[2]
-admin
-myviwo
when I request http://localhost/mysite/admin it redirect me to http://localhost/mysite/apps/admin/ and shows me the content of the admin directory, if I request http://localhost/mysite/admin/ it doesn't redirect me but it shows me the content of admin directory again which is correct. But I want:
http://localhost/mysite/admin
http://localhost/mysite/admin/
Both of the above URLs shows me the content of admin directory without redirecting me.
.htaccess [1]
RewriteEngine On
RewriteBase /
RewriteRule (.*) apps/$1 [L]
.htaccess [2]
RewriteEngine On
RewriteBase /
RewriteRule ^admin/?(.*)$ admin/$1 [L]
RewriteRule ^(.*)$ myviwo/$1 [L]
How can I achieve this?
In the admin directory, add an htaccess file with the following:
DirectorySlash Off
This makes it so mod_dir won't redirect requests for the directory admin. However, note that there's an important reason why directory slash is "on" by default:
Security Warning
Turning off the trailing slash redirect may result in an information disclosure. Consider a situation where mod_autoindex is active (Options +Indexes) and DirectoryIndex is set to a valid resource (say, index.html) and there's no other special handler defined for that URL. In this case a request with a trailing slash would show the index.html file. But a request without trailing slash would list the directory contents.
If that's ok with you, then that's all that you need.
Otherwise, you may need to add a special rule specifically for admin. In the .htaccess[1] file, add right below the rewrite base:
RewriteRule ^admin$ apps/admin/ [L]
EDIT: to make the above rule dynamic, you need to first check if it's a directory:
RewriteCond %{DOCUMENT_ROOT}/apps%{REQUEST_URI} -d
RewriteRule ^(.*[^/])$ apps/$1/ [L]

htaccess - rewrite URL to make querystring nice

I have a site with a folder, and a htaccess file within that folder. For the index.php file within that folder, I want to rewrite the querystring for a certain parameter, so that typing in this URL:
www.example.com/myfolder/myparameter
Behaves like this (ie makes $_GET['parameter'] = 'myparameter' in my code)
www.example.com/myfolder/index.php?parameter=myparameter
I have looked at many questions on StackOverflow, but have not managed to get this working. My code so far is
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*) [NC]
RewriteRule ^$ %0 [QSA]
But that just isn't working at all.
Please use this code
RewriteEngine on
RewriteRule (.*) index\.php?parameter=$1 [L,QSA]
RewriteEngine on
RewriteRule (^.*/)([^/]+)$ $1index\.php?parameter=$2 [L,QSA]
update
sorry use #somasundaram's answer. Per-directory .htaccess rewrite rules lose the directory prefix:
When using the rewrite engine in .htaccess files the per-directory prefix (which always is the same for a specific directory) is automatically removed for the RewriteRule pattern matching and automatically added after any relative (not starting with a slash or protocol name) substitution encounters the end of a rule set. See the RewriteBase directive for more information regarding what prefix will be added back to relative substitutions.
(from the apache docs)

Rewrite rules error

I want to rewrite a simple url, but without generating google errors
This code works :
RewriteEngine on
RewriteRule lieu/([0-9]+).* index.php?com=location&lID=$1 [L]
RewriteRule evenement/([0-9]+).* index.php?eID=$1 [L]
but i want to add R=301 flag for SEO
When i add [R=301,L] :
The requested URL /var/www/mysite/index.php was not found on this server.
I know that R=301 flag must be used with http://
but when i try the url is not rewritting
Apache tries to guess whether a path is a URI-path or a file-path, and it's guessing wrong. When you are internally rewriting, a file-path is perfectly fine, because it's all internal to the server. But when you are redirecting, apache incorrectly guesses that your target (the index.php?eID= ) is a file-path and it gets flagged to be handled by mod_alias as a redirect. By the time the redirect happens, it's malformed as a file-path instead of URI path. That's why you're getting the /var/www/mysite/ bit when you redirect.
Either add a RewriteBase to provide a URI base for relative URIs, or make your target an absolute URI:
RewriteEngine On
RewriteBase /
RewriteRule lieu/([0-9]+).* index.php?com=location&lID=$1 [R=301,L]
RewriteRule evenement/([0-9]+).* index.php?eID=$1 [L,R=301]
or
RewriteEngine on
RewriteRule lieu/([0-9]+).* /index.php?com=location&lID=$1 [L,R=301]
RewriteRule evenement/([0-9]+).* /index.php?eID=$1 [L,R=301]

.htaccess 301 redirect path and all child-paths

I want accesses to e.g. www.thisdomain.com/docs/path1/path2 to redirect to www.thatdomain.com/path1/path2
(Note that docs is not a part of the new path)
I have the following on www.thisdomain.com:
RewriteEngine on
RewriteRule ^docs/* http://www.domain.com/ [R=301,L]
If I access www.thisdomain.com/docs, it directs to www.thatdomain.com, but if I access a child-path like www.thisdomain.com/docs/path1/path2 it fails. Is it possible for the redirect to intercept the child-path access and redirect as I need? If so, any pointers?
Thanks.
With regular expressions, * means any number of the previous atom, which will match /docs and /docs/. Try this:
RewriteEngine on
RewriteRule ^docs$ http://www.domain.com/ [R=301,L,QSA]
RewriteRule ^docs/(.*) http://www.domain.com/$1 [R=301,L,QSA]
(QSA is query string append, so /docs/foo?bar=baz won't lose the ?bar=baz.)
According to section "Redirect Old domain to New domain using htaccess redirect" of the first Google result which I found searching for "htaccess redirect" (without the double quotes), this piece of code will suffice:
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
According to their description of the technique, it will redirect the directory the .htaccess file is placed in recursively (including any child paths), just as you intend. Of course, mod_rewrite needs to be present for the rewrite directives to work.

Resources