Htaccess some directory, subdirectory usage - apache

I want rewriting "cat/" => x.php
"cat/sample-title" => y.php?t=$1 with htaccess.
I writed rewrite rules;
RewriteRule ^cat/ x.php [L]
RewriteRule ^cat/(.*)$ y.php?s=$1 [L]
but always cat/ rewriting to y.php?s=$1

You're seeing two issues. The first is that both the first and second rules' patterns are matched by ^cat/, so the second one supplants the first after it is rewritten. You will need to anchor it with $, and also may want to permit an optional trailing / with /?.
Then, to differentiate it from the first, the second one must match one or more characters following the cat/, so instead of (.*), you should use (.+).
I would also change their order (though it is not strictly necessary in this case)
# Ensure one or more characters with .+
RewriteRule ^cat/(.+)$ y.php?s=$1 [L]
# Anchored with optional trailing /
RewriteRule ^cat/?$ x.php [L]

Related

remove //xx after directory in URL

I need to redirect all URLs like this:
example.com/podcasts//rebt
to
example.com/podcasts
I am trying to adjust this code to do both but I can't get it to work:
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(/{2,})(.*)$
RewriteRule . %1/%3 [R=301,L]
To remove //<something> at the end of the URL-path (eg. /podcasts//rebt to /podcasts, try the following instead at the top of the root .htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s([^?]+?)//
RewriteRule . %1 [R=301,L]
THE_REQUEST server variable contains the first line of the initial request headers (eg. GET /podcasts/rebt HTTP/1.1) and does not change when the request is internally rewritten (unlike REQUEST_URI).
The regex \s([^?]+?)// captures the part of the URL-path before the first instance of a double slash. Anything after and including the double slash, are discarded. This regex also ensures we do not inadvertently match against the query string (if any).
The %1 backreference contains the captured subpattern (ie. everything before the first double slash in the URL-path) from the preceding CondPattern.
Aside: Note that this will not work properly if the preceding URL-path maps to a physical directory, since it will result in two redirects. eg. /directory//something to /directory to /directory/ (by mod_dir). In this case, you should avoid removing the first trailing slash.
You should test first with a 302 (temporary) redirect to avoid any potential caching issues and only change to a 301 (permanent) redirect when you are sure it's working as intended. You should clear your browser cache before testing.
A look at your existing rule...
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_URI} ^(.*?)(/{2,})(.*)$
RewriteRule . %1/%3 [R=301,L]
This code is intended to reduce multiple slashes to single slashes in the URL-path, not remove the double slash and remaining path entirely. eg. /podcasts//rebt to /podcasts/rebt. However, since it checks against the REQUEST_URI server variable (which can change throughout the request) it may not work as intended.
Also, the condition that checks against the REQUEST_METHOD would seem to be redundant, unless you are erroneously POSTing to double-slashed URLs internally? A 301 redirect removes any POST data (since the browser converts it to GET) - hence why the check may be necessary in certain cases.

simple htaccess RewriteRule or redirect doesnt work properly

Trying to implement a simple .htaccess redirect
none of these work, any idea how to fix ?
RewriteRule ^/en/?productsublayout=products_horizon http://www.example.com/en/ [R=302,L]
Redirect 301 /en/?productsublayout=products_horizon http://www.example.com/en/
Read the documentation of mod_rewrite, as your code has two common problems, both which already have been addressed in the documenation.
Per-directory Rewrites
[..]
The removed prefix always ends with a slash, meaning the matching occurs against a string which never has a leading slash. Therefore, a Pattern with ^/ never matches in per-directory context.
and
If you wish to match against the hostname, port, or query string, use a RewriteCond with the %{HTTP_HOST}, %{SERVER_PORT}, or %{QUERY_STRING} variables respectively.
Besides this, keep in mind that the first argument of RewriteRule is a regex. The ? character has a special meaning in a regex.
You have to correct it like this:
RewriteCond %{QUERY_STRING} ^productsublayout=products_horizon$
RewriteRule ^en/$ /en/? [R=302,L]
Alternatively, if you have a recent version of Apache, use the QSD flag instead of a trailing ? behind the rewrite part.

Conditional rewrite in the regex, not with RewriteCond

I wrote a regex for a rewrite rule that includes a negative lookahead to replace the rewriteCond line, because WordPress only accepts two values: pattern -> substitution.
It should find findme.html _here, regardless of where it's requested to be:
mydomain.com/_here/findme.html
e.g.
(Sorry, I can't modify the swf which will request findme.html in the wrong places)
So, given findme.html could be requested to be in, e.g.:
mydomain.com/findme.html
mydomain.com/directory/findme.html
mydomain.com/directory/findme.html?someparam=3
The rewrite should make them all
mydomain.com/_here/findme.html
So, I made a rewrite rule that Wordpress will accept me as follow
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^.*(?!_here)/*findme\.html$ /_here/findme.html [R=301,L]
So it only matches URLs which doesn't contain "_here" in it, to prevent extra rewriting or a loop.
The problem is IT DOES loop.
What did I miss?
It looks to me like you want to move the .* that is before (?!_here) to after it because (?!_here) is a negative lookahead, so it check that the text _here does not come after it. What your regular expression is actually checking is whether your url starts with some character sequence that is not followed by _here, and _here is a character sequence not followed by _here. Then your rule becomes
RewriteRule ^(?!_here).*/*findme\.html$ /_here/findme.html [R=301,L]
Also, it looks like your pattern will exclude paths with subdirectories such as
mydomain.com/directory/subdirectory/findme.html
If you also want to include those, the pattern should be
RewriteRule ^(?!_here)(.*/)*findme\.html$ /_here/findme.html [R=301,L]

Apache URL Rewrite to domain.com/custom_url_name

Using Apache on a Red Hat server, I'm trying to rewrite the URL of a member's store on our website from:
domain.com/store.php?url=12345
to:
domain.com/12345
Using these rules, I can get it to work if I always remember to add an ending slash:
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^store/url/(.*)$ store.php?url=$1
RewriteRule ^(.*)/$ store.php?url=$1
domain.com/12345/ works,
but domain.com/12345 does not work.
Removing the slash in the last line of Rewrite code breaks a lot of stuff. Is there a way to get this to work both with or without that ending slash?
What if you made the slash optional? Furthermore, you probably to to specify something more specific than (.*), because domain.com/a/b/c/d/e will match. Instead, you can use a negated character class to specify everything other than a slash.
RewriteRule ^([^/]*)/?$ store.php?url=$1
Alternately, if you only want to capture numbers, you can use the \d shorthand class (which matches any digit) along with a + which specifies that at least one digit must be present:
RewriteRule ^(\d+)/?$ store.php?url=$1
Your attempt using ^(.*)$ fails because that would match any URL path. Use a more specific pattern than .*, maybe \d+ to allow only one or more digits:
RewriteRule ^(\d+)$ store.php?url=$1

.htaccess same url with or without /

I am doing a painful rewrite of many urls on a website I am currently working on, but I have noticed a small problem:
RewriteRule ^domains/transfer$ ./cart.php?gid=11 [L,NC]
This line with navigate if I go to:
http://my-site/domains/transfer
But it won't work with a trailing /:
http://my-site/domains/transfer/
Is there a way I can change the RewriteCode to take either argument and go to the same page. It seems wasteful to have the same line twice to include a '/'
Any help would be appreciated.
Cheers
Change the line to this:
RewriteRule ^domains/transfer/?$ ./cart.php?gid=11 [L,NC]
The magic is here: /? and that allows for the preceding character, in this case the slash (/), to be optional.
If you want something to come after the transfer, then remove the dollar sign ($) which marks the end of the allowable matching.
I would recommend you to allow just one URL form, the one with or without the trailing slash, and redirect if malformed:
# remove trailing slash
RewriteRule (.*)/$ /$1 [L,R=301]
# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ /$0/ [L,R=301]