Htaccess rewrite index with trailing slash or without - .htaccess

I am currently having a problem with my index url rewrite in my .htaccess file, I know if I use
RewriteRule ^profile/([^/]*)/?$ /profile.php?x=$1 [L]
I would be able to use www.example.com/profile/get or www.example.com/profile/get/ (with or without trailing slash)
But I would like www.example.com/get what I have so far is
RewriteRule ^([^/]*)\/$ /index.php?x=$1 [L]
But if I put a ? before the $ it errors any answers welcome

Making the trailing slash optional will lead to an infinite loop, since [^/]* will match anything that doesn't include a /, ie it would also match index.php?x=get
You can avoid this by making the rule apply conditionally, for example by testing the reqeust URI:
RewriteCond %{REQUEST_URI} !^/index\.php.*
RewriteRule ^([^/]*)\/?$ /index.php?x=$1 [L]
That way the rule can only apply in case the request URI doesn't start with /index.php

Related

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 file seems to pick up 'if contains' for a RewriteRule

We currently have a .htaccess RewriteRule that's incorrectly (or correctly as the rule is incorrect) redirecting a URL.
The Rule
RewriteRule ^holiday-ecards/?.*$ /appindex.php [L]
The desired redirects for this are:
http://domain.com/holiday-ecards/
http://domain.com/holiday-ecards/1/
http://domain.com/holiday-ecards/1/2
http://domain.com/holiday-ecards/1/2/3
However, it seems to also be redirecting the following, which is undesired:
http://domain.com/holiday-ecards-business/
EDIT
/appindex.php
This is taking care of the app routing and works as intended.
A number of ways you could do it, one would be setting a rewrite condition to not touch URI's that have holiday-ecards plus hyphen, like so:
RewriteCond %{REQUEST_URI} !^/holiday-ecards-.*$
RewriteRule ^holiday-ecards/?.*$ /appindex.php [L]
Not sure how many variations you have of URI's with holiday-ecards in them.
RewriteRule ^holiday-ecards/?.*$ /appindex.php [L]
(Note that this is an internal rewrite, not a redirect.)
The above RewriteRule pattern makes the slash after holiday-ecards optional (so it will also match holiday-ecards-business). However, in the example URLs that should be rewritten, the slash is mandatory. So, it would appear that you just need to make it mandatory (?), for example:
RewriteRule ^holiday-ecards/ /appindex.php [L]
The trailing pattern .*$ is superfluous.

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: If any of these directories requested, rewrite to script and pass the requested dir as variariable

I have a bunch of different blogs on my site i.e.
mysite.com/news
mysite.com/blog
mysite.com/bits
etc..
I use the same php script behind the scenes and want to keep the urls tidy so I've setup these rules:
# blogs
RewriteRule ^(bits|blog|news)(/?)$ news/?type=$1
RewriteRule ^(bits|blog|news)/[a-zA-Z0-9_-]+/([0-9]+)(/?)$ news/article.php?type=$1&id=$2
Now what's happening is that the 'type' is always coming through as news? I don't see why.
I would suggest adding a leading '/' to the URL your are redirecting to - also you don't need to wrap the trailing '/' in parentheses.
Also, put a [L] flag at the end of the rule to stop it looking at the rest of the .htaccess
Try this:
RewriteRule ^(bits|blog|news)/?$ /news/?type=$1 [L]
RewriteRule ^(bits|blog|news)/[a-zA-Z0-9_-]+/([0-9]+)/?$ /news/article.php?type=$1&id=$2 [L]
Prepending a RewriteCond of ..
RewriteCond %{QUERY_STRING} ^$
.. to each rule will also filter out any chance of URL's with a query string being parsed.

htaccess: Mediafire.com like urls

I'm trying to come up with some mod_rewrite to translate http://example.com/?7gudznrxdnu into http://example.com/view.php?id=7gudznrxdnu
But any other page will function properly such as http://example.com/contact and so on.
I think this will work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^[a-z0-9]+$
RewriteRule ^$ view.php?id=%{QUERY_STRING} [L]
If you want the rewrite to be shown in the browser's address field, you'll have to replace [L] with [L,R=301].
Explanation: The query-string (what's following the question mark) is not part of the URL that RewriteRule sees in its matching-pattern, therefore you can't check for question mark there. In my solution, I run the rule if and only if (RewriteCond) the query string consists solely of a-z and/or 0-9, and my rule only rewrites URLs ending with a slash (except for the query string). I redirect this to view.php?id=, and then append the query string to that.
Edit: Tested on my Apache-server, and I haven't found any bugs (yet).
You should try (in your .htaccess):
RewriteEngine On
RewriteRule ^\?([^/\.]+)?$ view.php?id=$1 [L]

Resources