Mirror a file in htaccess - .htaccess

I'm trying to work on making a new site and I want to be able to mirror a site. Below is an example:
User visits: https://example.com/items/{some child folder}
User sees this file mirrored: https://example.com/items/listing.php
I want user to be able to see that file, but, when doing so, it don't want it to redirect. Any ideas?
UPDATE
I found a solution to the above problem. However, I need another question fixed. How would I stop the file listing.php in the /products folder from following the redirect?
RewriteEngine On
RewriteRule ^products/(.*) index.php?name=$1 [NC,L]

How would I stop the file listing.php in the /products folder from following the redirect?
RewriteRule ^products/(.*) index.php?name=$1 [NC,L]
Be more specific in the regex. If your products don't contain dots in the URL-path then exclude dots in the regex. For example:
RewriteRule ^products/([^./]+)$ index.php?name=$1 [L]
The above assumes your product URLs are of the form /products/<something>. Where <something> cannot consist of dots or slashes (so naturally excludes listing.php) and must consist of "something", ie. not empty.
Unless you specifically need the NC flag then this potentially opens you up to duplicate content.
If you want to be explicit then include a condition (RewriteCond directive):
RewriteCond %{REQUEST_URI} !^/products/listing\.php$
RewriteRule ^products/([^/]+)$ index.php?name=$1 [L]
The REQUEST_URI server variable contains the root-relative URL-path, starting with a slash. The ! prefix on the CondPattern negates the regex.
Or, use a negative lookahead in the RewriteRule pattern, without using a condition. For example:
RewriteRule ^products/(?!listing\.php)([^/]+)$ index.php?name=$1 [L]
Reference:
https://httpd.apache.org/docs/current/rewrite/intro.html
https://httpd.apache.org/docs/current/mod/mod_rewrite.html

Related

rewrite urls , not all urls works

I have blog and I want to rewrite the link
site.com/blog -> for the blog index blog_posts.php
site.com/blog/{post-slug} -> for the single_post.php?post-slug={post-slug}
site.com/blog/topic/{topic} -> for the filtered_posts.php?topic={topic}
my problem is when I access to a post or a topic its access to site.com/blog blog_posts.php
I think because when access to blog_posts.php and write after it any think like kbjsflgjdouihaiufhslkdf access to blog_posts.php
my try
RewriteRule ^blog/([0-9a-zA-Zأ-ي-]+)/ single_post.php?post-slug=$1 [NC,L]
RewriteRule ^blog/ blog_posts.php [NC,L]
RewriteRule ^blog/topic/([0-9a-zA-Zأ-ي-]+)/ filtered_posts.php?topic=$1 [NC,L]
and if there are any thing that tech all about the .htaccess please gimme a link
RewriteRule ^blog/([0-9a-zA-Zأ-ي-]+)/ single_post.php?post-slug=$1 [NC,L]
RewriteRule ^blog/ blog_posts.php [NC,L]
RewriteRule ^blog/topic/([0-9a-zA-Zأ-ي-]+)/ filtered_posts.php?topic=$1 [NC,L]
You need to change the order of the directives and/or make the regex more specific. Order is important - the most specific regex need to be first.
For instance, your first rule ^blog/([0-9a-zA-Zأ-ي-]+)/ will naturally match any URL of the form /blog/topic/<something>, so the request is rewritten to single_post.php and the 3rd rule is never processed.
And your 2nd rule (^blog/), matches anything else that simply starts /blog, so again, the 3rd rule would never be processed.
Your example URLs, eg /blog/{post-slug} do not include a slash suffix, however, the directives you have written look as if you are trying to match one? Although you are not using any end-of-string anchor on the regex, so you are probably matching too much. For instance, the regex ^blog/([0-9a-zA-Zأ-ي-]+)/ will match a URL of the form /blog/{post-slug}/1234/something/etc which is probably not the intention (if it is the intention then it's still a fault as you potentially have a duplicate content issue).
I'm assuming your URLs are as per your example - no trailing slash.
I also queried (in comments) whether /blog a physical directory on the filesystem? If it is then your first example URL, ie. /blog is not canonical since mod_dir will implicitly trigger a 301 redirect to append the trailing slash. I'm assuming this is a physical directory and the canonical URL is actually /blog/, not /blog.
Try the following instead:
RewriteRule ^blog/topic/([0-9a-zA-Zأ-ي-]+)$ filtered_posts.php?topic=$1 [L]
RewriteRule ^blog/([0-9a-zA-Zأ-ي-]+)$ single_post.php?post-slug=$1 [L]
RewriteRule ^blog/$ blog_posts.php [L]
I also remove the NC (nocase) flag. You are already checking for both lowercase and uppercase letters in the regex, but if you are also allowing uppercase letters in other parts of the URL then you potentially have a duplicate content (non-canonical) URL issue that would need to be resolved.
Reference:
https://httpd.apache.org/docs/current/rewrite/intro.html
https://httpd.apache.org/docs/current/mod/mod_rewrite.html

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]

Error with .htaccess

I need to open my file banclub.html on address http://site.ru/products/love-is
I was create .htaccess file:
RewriteEngine on
RewriteRule ^/banclub.html$ /products/love-is
What's wrong?
Is the incoming URL http://site.ru/products/love-is, and you want to send it to http://site.ru/banclub.html? Or vice-versa? Assuming the first case:
RewriteEngine On
RewriteRule ^products/love-is$ /banclub.html
Note that the RewriteRule has already stripped off the leading /. This rule will pass along any Query String in the URI (?var=value&var=value etc.) to the new URI. You can suppress that by adding just a ? after .html . If "love-is" is a directory, and the user might have a trailing /, change that line to
RewriteRule ^products/love-is/?$ /banclub.html
If you want to tell search engines and visitors to update their indexes and bookmarks, add [R=301] at the end of the RewriteRule line.

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.

mod rewrite exclude all but php

Is it possible to edit htacces in such a way that only the following url is rewritten and the rest isn't?
http://www.example.com/index.php?x=foobar
to
http://www.example.com/foobar/
I want the pages not having x=... as a variable to behave normally
I got the following but that doesn't work
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)/ index.php?x=$1
RewriteCond $1 !\.(js|ico|gif|jpg|png|css|html|swf|mp3|wav|txt)$
Who can help me?
First off, the RewriteCond must be put before the RewriteRule to which it belongs.
But I think that you need another approach for your case, something like this:
RewriteRule (.*)\.php - [PT,L]
RewriteRule ^(.*)/$ index.php?x=$1
The first rule Passes Through (PT) every PHP page, so the second rule is only applied to all non-PHP requests.
That second rule only applies to a "simple path", no matter if this path has a dot in it or not (e.g. hello.gif/ will match, too).
If this does not work for you, then you might consider one of these points to start further research:
the pattern ([^\.]*) matches everything that does not have a dot in it
see RewriteCond to skip rule if file or directory exists for RewriteConds where the following RewriteRule is only used if the request does not point to an existing file or directory
Hope this helps.

Resources