htaccess redirect fake folder structure - .htaccess

Trying to create fake folder structure.
Essentially what I want is any request to http://www.example.com/** (where ** equals any two letters) to redirect to http://www.example.com/folder/index.php?var=** (where ** equals the two letters from the first link)
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^/(.*)$ http://www.example.com/folder/index.php?var=$1
This is what I have right now, but it isn't working. Any ideas?

Remove the leading / from the RewriteRule. The expression matched on the left side should not begin with / as a REQUEST_URI would. You can also omit the entire http://example.com from the right side, since you are rewriting on the same domain.
RewriteEngine On
# Rewrite 2 characters into var
RewriteRule ^(.{2})$ folder/index.php?var=$1 [L,R]
If it should match only alpha chars, it is recommended to limit the expression further (beyond .{2}, which matches 2 of any character)
# Only rewrite on exactly 2 alpha chars...
RewriteRule ^([A-Za-z]{2})$ folder/index.php?var=$1 [L,R]

Related

How can I Rewrite Query String to Path and remove %20 in url with dash (-)? [duplicate]

So here's my problem. I took over a site that has has a bunch of pages indexed that have %20 indexed in Google. This is simply because the person decided to just use the tag name as the title and url slug. So, the urls were something like this:
http://www.test.com/tag/bob%20hope
http://www.test.com/tag/bob%20hope%20is%20funny
I have added a new field for the url slug and string replaced all spaces with dashes. While I have no problem linking to these new pages and getting the data, I need to 301 redirect the old URLs to the new URLs, which would be something like:
http://www.test.com/tag/bob-hope
http://www.test.com/tag/bob-hope-is-funny
So, it needs to be able to account for multiple spaces. Any questions? :)
Use these rules in your .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
# keep replacing space to hyphen until there is no space use internal rewrite
RewriteRule ^([^\s%20]*)[\s%20]+(.*)$ $1-$2 [E=NOSPACE:1]
# when there is no space make an external redirection
RewriteCond %{ENV:NOSPACE} =1
RewriteRule ^([^\s%20]+)$ $1 [R=301,L]
This will replace all space characters (\s or %20) to hyphen -
So a URI of /tag/bob%20hope%20is%20funny will become /tag/bob-hope-is-funny with 301
Brief Explanation: If there are more than 1 space in URI then 1st RewriteRule is fired recursively replacing each space character with hyphen - until there is no space left. This rule will only rewrite internally.
Once no space is left 2nd RewriteRule is fired which just uses a 301 redirect to the converted URI.
Building on #anhubhava's answer, it's close, but that will also match %,2 or 0 in the URL, and it can cause a loop on apache 2.2 if you don't use the DPI parameter. The full script should look like this:
Options FollowSymlinks MultiViews
RewriteEngine on
RewriteBase /
# keep replacing space to hyphen until there is no space use internal rewrite
RewriteRule ^([^\s%20]*)(?:\s|%20)+(.*)$ $1-$2 [N,E=NOSPACE:1,DPI]
# when there is no space make an external redirection
RewriteCond %{ENV:NOSPACE} =1
RewriteRule ^([^\s%20]+)$ $1 [R=301,L]
I've also added the N (Next) parameter as this then forces the rules to be re-evaluated from the start straight after this rule if it matches. If this isn't there, you can get problems if you're using apache as a reverse proxy as it's unlikely that it'll get to the end of the rewrites before something else happens.

HTACCESS How to "cut" URL at one point

I am new to .htaccess and I don't understand it well. Recently I have built the following code:
RewriteEngine On
RewriteCond %{HTTP_HOST} (.*)
RewriteCond %{REQUEST_URI} /api/v2/
RewriteRule ^api/v2(.*) /api/v2/api.php?input=$1
This was in the root public folder (example.com/.htaccess). But now I have to create second Rewrite and I want to make .htaccess file in example.com/api/v2/ folder. I tried to remove /api/v2/ part in each Rewrite Rule, but only thing I got was error 500.
What I want to achieve:
If someone uses this link: https://example.com/api/v2/test/test/123, I'd like to make it into https://example.com/api/v2/api?input=test/test/123 with .htaccess located in example.com/api/v2 folder.
Addressing your existing rule first:
RewriteCond %{HTTP_HOST} (.*)
RewriteCond %{REQUEST_URI} /api/v2/
RewriteRule ^api/v2(.*) /api/v2/api.php?input=$1
The first RewriteCond (condition) is entirely superfluous and can simply be removed. The second condition simply asserts that there is a slash after the v2 and this can be merged with the RewritRule pattern. So, the above is equivalent to a single RewriteRule directive as follows:
RewriteRule ^api/v2(/.*) /api/v2/api.php?input=$1 [L]
This would internally rewrite the request from /api/v2/test/test/123 to /api/v2/api.php?input=/test/test/123 - note the slash prefix on the input URL parameter value.
However, unless you have another .htaccess file in a subdirectory that also contains mod_rewrite directives then this will create a rewrite loop (500 error).
Also note that you should probably include the L flag here to prevent the request being further rewritten (if you have other directives).
If someone uses this link: https://example.com/api/v2/test/test/123, I'd like to make it into https://example.com/api/v2/api?input=test/test/123 with .htaccess located in example.com/api/v2 folder.
I assume /api? is a typo and this should be /api.php?. Note also that the slash is omitted from the start of the URL parameter value (different to the rule above).
I tried to remove /api/v2/ part in each Rewrite Rule, but only thing I got was error 500.
This is the right idea, however, you need to be careful of rewrite loops (ie. 500 error response) since the rewritten URL is likely matching the regex you are trying to rewrite.
Try the following instead in the /api/v2/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_URI} !api\.php$
RewriteRule (.*) api.php?input=$1 [L]
The preceding RewriteCond directive checks that the request is not already for api.php, thus avoiding a rewrite loop, since the pattern .* will naturally match anything, including api.php itself.
You could avoid the additional condition by making the regex more specific. For example, if the requested URL-path cannot contain a dot then the above RewriteCond and RewriteRule directives can be written as a single directive:
RewriteRule ^([^.]*)$ api.php?input=$1 [L]
The regex [^.]* matches anything except a dot, so avoids matching api.php.
Alternatively, only match the characters that are permitted. For example, lowercase a-z, digits and slashes (which naturally excludes the dot), which covers your test string test/test/123:
RewriteRule ^([a-z0-9/]*)$ api.php?input=$1 [L]
Or, if there should always be 3 path segments, /<letters>/<letters>/<digits>, then be specific:
RewriteRule ^([a-z]+/[a-z]+/\d+)$ api.php?input=$1 [L]

htaccess Rewriterule for multiple Parameters

I have an URL that Looks like:
https://www.example.com/project/api/read.php?uid=1234567&dvc=ABCDE
I would like to establish a rewrite rule that turns the URL in:
https://www.example.com/project/api/read/1234567/ABCDE
My try Looks like this:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^read/([0-9a-zA-Z_-]*)([0-9a-zA-Z_-]*)$ read.php?uid=$1&dvc=$2 [NC,L,QSA]
RewriteRule ^read/([0-9a-zA-Z_-]*)([0-9a-zA-Z_-]*)$ read.php?uid=$1&dvc=$2 [NC,L,QSA]
You are missing a slash between the path segments in the RewriteRule pattern.
Assuming your .htaccess file is located inside the /project/api subdirectory (as implied by your directive) then try the following instead:
# MultiViews must be disabled for the rewrite to work
Options -MultiViews
# Turn on the rewriting engine
RewriteEngine On
RewriteRule ^read/([\w-]+)/([\w-]*)$ read.php?uid=$1&dvc=$2 [L,QSA]
\w is a shorthand character class that is the same as [0-9a-zA-Z_], to which we add the hyphen (-).
Since the URL /read maps directly to the file /read.php MultiViews must be disabled, otherwise mod_negotiation "rewrites" the request to /read.php (before mod_rewrite) without any URL parameters.
Note that I changed the quantifier from * to + in the first path segment, since this would seem to be mandatory in your URL?
Your example URL (/read/1234567/ABCDE) shows digits in the first path segment and letters in the second. If this is an accurate reflection of the type of URL then the regex should be made more restrictive to check just for this data type.
Aside:
RewriteEngine On # Turn on the rewriting engine
Note that Apache does not support line-end comments. This may be "OK" in this instance because the text after RewriteEngine On is simply ignored. In other cases you might get a 500 internal server error.

Mod rewrite to redirect except on a certain page

Trying to write a rewrite rule in my htaccess so that any request to /en/shop/index.html or /fr/shop/index.html stays on the server, but if the user goes to any other page it redirects to a different server. Here's what I've got so far and it doesn't work.
RewriteRule ^(.*)/shop/(.*) [L]
RewriteRule ^(.*)$ http://www.newwebsite.com/$1 [R=301]
Add a dash to tell the first RewriteRule that you want the matches to be passed through unchanged:
RewriteRule ^.*/shop(/.*)?$ - [L]
I also removed the first set of parentheses since you're not using the results of the match so there's no need to store the matched patterns. I assumed you might need to match /shop without a trailing slash so that's why the (/.*)? section is there.

htaccess redirect with php variables ...

I'm using this format in my htaccess to redirect several pages/links:
RewriteEngine On
RewriteRule special.php http://www.mysite.com [R=301]
...
...
RewriteRule http://www.mysite.com/special.php?t=master http://www.mysite.com/index.php?q=former [R=301, L]
I noticed, first, that only the top line is catching anything, and in fact the others, like the bottom line, did nothing until I put in that top line. Any ideas why?
Second, mysite.com/special.php?t=grave is redirected, by the above top line, to mysite.com/?t=grave , thus retaining the variables in the URL. I don't want this, I simply want it to go to mysite.com with no variables. How do I do this?
Thanks,
Derek
First, your first rule catches any URI with special.php in it, even if it is followed by a bunch of characters. To limit it to only and exactly special.php, and to make sure the query string is discarded, change it to
RewriteRule ^special.php$ http://www.mysite.com/? [L, R=301]
Secondly, rewrite rules only match the part after http://www.mysite.com/ (note the last slash) and before the query string (the part after the question mark). So if you change the format of those rules to
RewriteCond %{QUERY_STRING} t=master
RewriteRule ^special.php$ index.php?q=former [R=301, L]
you should be good to go.

Resources